{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding=utf-8\n",
    "import os.path as osp\n",
    "import os\n",
    "import copy\n",
    "import matplotlib.pyplot as plt\n",
    "import torch\n",
    "from torch.nn import Linear\n",
    "from sklearn.metrics import average_precision_score, roc_auc_score\n",
    "from torch_geometric.data import TemporalData\n",
    "from torch_geometric.datasets import JODIEDataset\n",
    "from torch_geometric.datasets import ICEWS18\n",
    "from torch_geometric.nn import TGNMemory, TransformerConv\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "from torch_geometric.nn.models.tgn import (LastNeighborLoader, IdentityMessage, MeanAggregator,\n",
    "                                           LastAggregator)\n",
    "from torch_geometric import *\n",
    "from torch_geometric.utils import negative_sampling\n",
    "from tqdm import tqdm\n",
    "import networkx as nx\n",
    "import numpy as np\n",
    "import math\n",
    "import copy\n",
    "import re\n",
    "import time\n",
    "import json\n",
    "import pandas as pd\n",
    "from random import choice\n",
    "import gc\n",
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "# device='cpu'\n",
    "# msg structure:      [src_node_feature,edge_attr,dst_node_feature]\n",
    "\n",
    "# compute the best partition \n",
    "import datetime\n",
    "# import community as community_louvain\n",
    "\n",
    "import xxhash\n",
    "#  Find the edge index which the edge vector is corresponding to\n",
    "def tensor_find(t,x):\n",
    "    t_np=t.cpu().numpy()\n",
    "    idx=np.argwhere(t_np==x)\n",
    "    return idx[0][0]+1\n",
    "\n",
    "\n",
    "def std(t):\n",
    "    t = np.array(t)\n",
    "    return np.std(t)\n",
    "\n",
    "\n",
    "def var(t):\n",
    "    t = np.array(t)\n",
    "    return np.var(t)\n",
    "\n",
    "\n",
    "def mean(t):\n",
    "    t = np.array(t)\n",
    "    return np.mean(t)\n",
    "\n",
    "def hashgen(l):\n",
    "    \"\"\"Generate a single hash value from a list. @l is a list of\n",
    "    string values, which can be properties of a node/edge. This\n",
    "    function returns a single hashed integer value.\"\"\"\n",
    "    hasher = xxhash.xxh64()\n",
    "    for e in l:\n",
    "        hasher.update(e)\n",
    "    return hasher.intdigest()\n",
    "\n",
    "\n",
    "def cal_pos_edges_loss(link_pred_ratio):\n",
    "    loss=[]\n",
    "    for i in link_pred_ratio:\n",
    "        loss.append(criterion(i,torch.ones(1)))\n",
    "    return torch.tensor(loss)\n",
    "\n",
    "def cal_pos_edges_loss_multiclass(link_pred_ratio,labels):\n",
    "    loss=[] \n",
    "    for i in range(len(link_pred_ratio)):\n",
    "        loss.append(criterion(link_pred_ratio[i].reshape(1,-1),labels[i].reshape(-1)))\n",
    "    return torch.tensor(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cal_pos_edges_loss_autoencoder(decoded,msg):\n",
    "    loss=[] \n",
    "    for i in range(len(decoded)):\n",
    "        loss.append(criterion(decoded[i].reshape(-1),msg[i].reshape(-1)))\n",
    "    return torch.tensor(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime, timezone\n",
    "import time\n",
    "import pytz\n",
    "from time import mktime\n",
    "from datetime import datetime\n",
    "import time\n",
    "def ns_time_to_datetime(ns):\n",
    "    \"\"\"\n",
    "    :param ns: int nano timestamp\n",
    "    :return: datetime   format: 2013-10-10 23:40:00.000000000\n",
    "    \"\"\"\n",
    "    dt = datetime.fromtimestamp(int(ns) // 1000000000)\n",
    "    s = dt.strftime('%Y-%m-%d %H:%M:%S')\n",
    "    s += '.' + str(int(int(ns) % 1000000000)).zfill(9)\n",
    "    return s\n",
    "\n",
    "def ns_time_to_datetime_US(ns):\n",
    "    \"\"\"\n",
    "    :param ns: int nano timestamp\n",
    "    :return: datetime   format: 2013-10-10 23:40:00.000000000\n",
    "    \"\"\"\n",
    "    tz = pytz.timezone('US/Eastern')\n",
    "    dt = pytz.datetime.datetime.fromtimestamp(int(ns) // 1000000000, tz)\n",
    "    s = dt.strftime('%Y-%m-%d %H:%M:%S')\n",
    "    s += '.' + str(int(int(ns) % 1000000000)).zfill(9)\n",
    "    return s\n",
    "\n",
    "def time_to_datetime_US(s):\n",
    "    \"\"\"\n",
    "    :param ns: int nano timestamp\n",
    "    :return: datetime   format: 2013-10-10 23:40:00\n",
    "    \"\"\"\n",
    "    tz = pytz.timezone('US/Eastern')\n",
    "    dt = pytz.datetime.datetime.fromtimestamp(int(s), tz)\n",
    "    s = dt.strftime('%Y-%m-%d %H:%M:%S')\n",
    "\n",
    "    return s\n",
    "\n",
    "def datetime_to_ns_time(date):\n",
    "    \"\"\"\n",
    "    :param date: str   format: %Y-%m-%d %H:%M:%S   e.g. 2013-10-10 23:40:00\n",
    "    :return: nano timestamp\n",
    "    \"\"\"\n",
    "    timeArray = time.strptime(date, \"%Y-%m-%d %H:%M:%S\")\n",
    "    timeStamp = int(time.mktime(timeArray))\n",
    "    timeStamp = timeStamp * 1000000000\n",
    "    return timeStamp\n",
    "\n",
    "def datetime_to_ns_time_US(date):\n",
    "    \"\"\"\n",
    "    :param date: str   format: %Y-%m-%d %H:%M:%S   e.g. 2013-10-10 23:40:00\n",
    "    :return: nano timestamp\n",
    "    \"\"\"\n",
    "    tz = pytz.timezone('US/Eastern')\n",
    "    timeArray = time.strptime(date, \"%Y-%m-%d %H:%M:%S\")\n",
    "    dt = datetime.fromtimestamp(mktime(timeArray))\n",
    "    timestamp = tz.localize(dt)\n",
    "    timestamp = timestamp.timestamp()\n",
    "    timeStamp = timestamp * 1000000000\n",
    "    return int(timeStamp)\n",
    "\n",
    "def datetime_to_timestamp_US(date):\n",
    "    \"\"\"\n",
    "    :param date: str   format: %Y-%m-%d %H:%M:%S   e.g. 2013-10-10 23:40:00\n",
    "    :return: nano timestamp\n",
    "    \"\"\"\n",
    "    tz = pytz.timezone('US/Eastern')\n",
    "    timeArray = time.strptime(date, \"%Y-%m-%d %H:%M:%S\")\n",
    "    dt = datetime.fromtimestamp(mktime(timeArray))\n",
    "    timestamp = tz.localize(dt)\n",
    "    timestamp = timestamp.timestamp()\n",
    "    timeStamp = timestamp\n",
    "    return int(timeStamp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "rel2id={1: 'EVENT_CONNECT',\n",
    " 'EVENT_CONNECT': 1,\n",
    " 2: 'EVENT_EXECUTE',\n",
    " 'EVENT_EXECUTE': 2,\n",
    " 3: 'EVENT_OPEN',\n",
    " 'EVENT_OPEN': 3,\n",
    " 4: 'EVENT_READ',\n",
    " 'EVENT_READ': 4,\n",
    " 5: 'EVENT_RECVFROM',\n",
    " 'EVENT_RECVFROM': 5,\n",
    " 6: 'EVENT_RECVMSG',\n",
    " 'EVENT_RECVMSG': 6,\n",
    " 7: 'EVENT_SENDMSG',\n",
    " 'EVENT_SENDMSG': 7,\n",
    " 8: 'EVENT_SENDTO',\n",
    " 'EVENT_SENDTO': 8,\n",
    " 9: 'EVENT_WRITE',\n",
    " 'EVENT_WRITE': 9}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import psycopg2\n",
    "\n",
    "from psycopg2 import extras as ex\n",
    "connect = psycopg2.connect(database = 'tc_theia_dataset_db',\n",
    "                           host = '/var/run/postgresql/',\n",
    "                           user = 'postgres',\n",
    "                           password = 'postgres',\n",
    "                           port = '5432'\n",
    "                          )\n",
    "\n",
    "cur = connect.cursor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Constructing the map for nodeid to msg\n",
    "sql=\"select * from node2id ORDER BY index_id;\"\n",
    "cur.execute(sql)\n",
    "rows = cur.fetchall()\n",
    "\n",
    "nodeid2msg={}  # nodeid => msg and node hash => nodeid\n",
    "for i in rows:\n",
    "    nodeid2msg[i[0]]=i[-1]\n",
    "    nodeid2msg[i[-1]]={i[1]:i[2]}  #0-828297"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "graph_4_3=torch.load(\"./train_graph/graph_4_3.TemporalData.simple\").to(device=device)\n",
    "graph_4_4=torch.load(\"./train_graph/graph_4_4.TemporalData.simple\").to(device=device)\n",
    "graph_4_5=torch.load(\"./train_graph/graph_4_5.TemporalData.simple\").to(device=device)\n",
    "graph_4_9=torch.load(\"./train_graph/graph_4_9.TemporalData.simple\").to(device=device)\n",
    "\n",
    "graph_4_10=torch.load(\"./train_graph/graph_4_10.TemporalData.simple\").to(device=device)\n",
    "graph_4_11=torch.load(\"./train_graph/graph_4_11.TemporalData.simple\").to(device=device)\n",
    "graph_4_12=torch.load(\"./train_graph/graph_4_12.TemporalData.simple\").to(device=device)\n",
    "graph_4_13=torch.load(\"./train_graph/graph_4_13.TemporalData.simple\").to(device=device)\n",
    "train_data=graph_4_10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[828311, 828304, 828187, 746145, 815985, 826308, 826255, 826255]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[\n",
    "    graph_4_3.num_nodes,\n",
    "    graph_4_4.num_nodes,\n",
    "    graph_4_5.num_nodes,\n",
    "    graph_4_9.num_nodes,\n",
    "    graph_4_10.num_nodes,\n",
    "    graph_4_11.num_nodes,\n",
    "    graph_4_12.num_nodes,\n",
    "    graph_4_13.num_nodes,\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "max_node_num = 828398 \n",
    "min_dst_idx, max_dst_idx = 0, max_node_num\n",
    "neighbor_loader = LastNeighborLoader(max_node_num, size=20, device=device)\n",
    "\n",
    "class GraphAttentionEmbedding(torch.nn.Module):\n",
    "    def __init__(self, in_channels, out_channels, msg_dim, time_enc):\n",
    "        super(GraphAttentionEmbedding, self).__init__()\n",
    "        self.time_enc = time_enc\n",
    "        edge_dim = msg_dim + time_enc.out_channels\n",
    "        self.conv = TransformerConv(in_channels, out_channels, heads=8,\n",
    "                                    dropout=0.0, edge_dim=edge_dim)\n",
    "        self.conv2 = TransformerConv(in_channels*8, out_channels,heads=1, concat=False,\n",
    "                             dropout=0.0, edge_dim=edge_dim)\n",
    "\n",
    "    def forward(self, x, last_update, edge_index, t, msg):\n",
    "        last_update.to(device)\n",
    "        x = x.to(device)\n",
    "        t = t.to(device)\n",
    "        rel_t = last_update[edge_index[0]] - t\n",
    "        rel_t_enc = self.time_enc(rel_t.to(x.dtype))\n",
    "        edge_attr = torch.cat([rel_t_enc, msg], dim=-1)\n",
    "        x = F.relu(self.conv(x, edge_index, edge_attr))\n",
    "        x = F.relu(self.conv2(x, edge_index, edge_attr))\n",
    "        return x\n",
    "\n",
    "class LinkPredictor(torch.nn.Module):\n",
    "    def __init__(self, in_channels):\n",
    "        super(LinkPredictor, self).__init__()\n",
    "        self.lin_src = Linear(in_channels, in_channels*2)\n",
    "        self.lin_dst = Linear(in_channels, in_channels*2)\n",
    "        \n",
    "        self.lin_seq = nn.Sequential(\n",
    "            \n",
    "            Linear(in_channels*4, in_channels*8),\n",
    "            torch.nn.Dropout(0.5),\n",
    "            nn.Tanh(),\n",
    "            Linear(in_channels*8, in_channels*2),\n",
    "            torch.nn.Dropout(0.5),\n",
    "            nn.Tanh(),\n",
    "            Linear(in_channels*2, int(in_channels//2)),\n",
    "            torch.nn.Dropout(0.5),\n",
    "            nn.Tanh(),\n",
    "            Linear(int(in_channels//2), train_data.msg.shape[1]-32)                   \n",
    "        )\n",
    "        \n",
    "    def forward(self, z_src, z_dst):\n",
    "        h = torch.cat([self.lin_src(z_src) , self.lin_dst(z_dst)],dim=-1)      \n",
    "         \n",
    "        h = self.lin_seq (h)\n",
    "        \n",
    "        return h\n",
    "\n",
    "\n",
    "memory_dim = time_dim = embedding_dim = 100\n",
    "\n",
    "memory = TGNMemory(\n",
    "    max_node_num,\n",
    "    train_data.msg.size(-1),\n",
    "    memory_dim,\n",
    "    time_dim,\n",
    "    message_module=IdentityMessage(train_data.msg.size(-1), memory_dim, time_dim),\n",
    "    aggregator_module=LastAggregator(),\n",
    ").to(device)\n",
    "\n",
    "gnn = GraphAttentionEmbedding(\n",
    "    in_channels=memory_dim,\n",
    "    out_channels=embedding_dim,\n",
    "    msg_dim=train_data.msg.size(-1),\n",
    "    time_enc=memory.time_enc,\n",
    ").to(device)\n",
    "\n",
    "link_pred = LinkPredictor(in_channels=embedding_dim).to(device)\n",
    "\n",
    "optimizer = torch.optim.Adam(\n",
    "    set(memory.parameters()) | set(gnn.parameters())\n",
    "    | set(link_pred.parameters()), lr=0.00005, eps=1e-08,weight_decay=0.01)\n",
    "\n",
    "\n",
    "# scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1)\n",
    "criterion = nn.CrossEntropyLoss()\n",
    "\n",
    "# Helper vector to map global node indices to local ones.\n",
    "assoc = torch.empty(max_node_num, dtype=torch.long, device=device)\n",
    "\n",
    "\n",
    "saved_nodes=set()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "BATCH=1024\n",
    "def train(train_data):\n",
    "\n",
    "    \n",
    "    memory.train()\n",
    "    gnn.train()\n",
    "    link_pred.train()\n",
    "\n",
    "    memory.reset_state()  # Start with a fresh memory.\n",
    "    neighbor_loader.reset_state()  # Start with an empty graph.\n",
    "    saved_nodes=set()\n",
    "\n",
    "    total_loss = 0\n",
    "    total_batchs=len(train_data.t)//BATCH +1\n",
    "    batch_index=0\n",
    "#     print(\"train_before_stage_data:\",train_data)\n",
    "    for batch in train_data.seq_batches(batch_size=BATCH):\n",
    "        start = time.perf_counter()\n",
    "        optimizer.zero_grad()\n",
    "\n",
    "        src, pos_dst, t, msg = batch.src, batch.dst, batch.t, batch.msg        \n",
    "        \n",
    "        n_id = torch.cat([src, pos_dst]).unique()\n",
    "#         n_id = torch.cat([src, pos_dst, neg_src, neg_dst]).unique()\n",
    "        n_id, edge_index, e_id = neighbor_loader(n_id)\n",
    "        assoc[n_id] = torch.arange(n_id.size(0), device=device)\n",
    "\n",
    "        # Get updated memory of all nodes involved in the computation.\n",
    "        z, last_update = memory(n_id)\n",
    "        \n",
    "#         print(f\"{n_id=}\")\n",
    "#         print(f\"{edge_index=}\")\n",
    "#         print(f\"{train_data.msg[e_id]=}\")\n",
    "      \n",
    "        z = gnn(z, last_update, edge_index, train_data.t[e_id], train_data.msg[e_id])\n",
    "        \n",
    "        pos_out = link_pred(z[assoc[src]], z[assoc[pos_dst]])       \n",
    "\n",
    "        y_pred = torch.cat([pos_out], dim=0)\n",
    "        \n",
    "#         y_true = torch.cat([torch.zeros(pos_out.size(0),1),torch.ones(neg_out.size(0),1)], dim=0)\n",
    "        y_true=[]\n",
    "        for m in msg:\n",
    "            l=tensor_find(m[16:-16],1)-1\n",
    "            y_true.append(l)           \n",
    "          \n",
    "        y_true = torch.tensor(y_true)\n",
    "        y_true=y_true.reshape(-1).to(torch.long).to(device=device)\n",
    "        \n",
    "        loss = criterion(y_pred, y_true)\n",
    "        \n",
    "\n",
    "#         loss = criterion(pos_out, torch.ones_like(pos_out))\n",
    "#         loss += criterion(neg_out, torch.zeros_like(neg_out))\n",
    "\n",
    "        # Update memory and neighbor loader with ground-truth state.\n",
    "        memory.update_state(src, pos_dst, t, msg)\n",
    "        neighbor_loader.insert(src, pos_dst)\n",
    "        \n",
    "#         for i in range(len(src)):\n",
    "#             saved_nodes.add(int(src[i]))\n",
    "#             saved_nodes.add(int(pos_dst[i]))\n",
    "\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        memory.detach()\n",
    "#         print(z.shape)\n",
    "        total_loss += float(loss) * batch.num_events\n",
    "        batch_index+=1\n",
    "        end = time.perf_counter()\n",
    "#         print(f\"current epoch process:{(batch_index/total_batchs):.4f}%   cost time:{(end-start):.2f}\")\n",
    "#     print(\"trained_stage_data:\",train_data)\n",
    "    return total_loss / train_data.num_events\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Start to train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  0%|                                                                                                   | 0/50 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 01, Loss: 0.5987\n",
      "  Epoch: 01, Loss: 0.2902\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  2%|█▊                                                                                      | 1/50 [04:42<3:50:27, 282.19s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 01, Loss: 0.3440\n",
      "  Epoch: 02, Loss: 0.2492\n",
      "  Epoch: 02, Loss: 0.2633\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  4%|███▌                                                                                    | 2/50 [09:25<3:46:12, 282.76s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 02, Loss: 0.3091\n",
      "  Epoch: 03, Loss: 0.2433\n",
      "  Epoch: 03, Loss: 0.2526\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  6%|█████▎                                                                                  | 3/50 [14:08<3:41:48, 283.17s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 03, Loss: 0.3456\n",
      "  Epoch: 04, Loss: 0.2386\n",
      "  Epoch: 04, Loss: 0.2553\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  8%|███████                                                                                 | 4/50 [18:52<3:37:10, 283.27s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 04, Loss: 0.3307\n",
      "  Epoch: 05, Loss: 0.2392\n",
      "  Epoch: 05, Loss: 0.2586\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 10%|████████▊                                                                               | 5/50 [23:35<3:32:32, 283.38s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 05, Loss: 0.3321\n",
      "  Epoch: 06, Loss: 0.2475\n",
      "  Epoch: 06, Loss: 0.2702\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 12%|██████████▌                                                                             | 6/50 [28:20<3:27:59, 283.61s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 06, Loss: 0.3207\n",
      "  Epoch: 07, Loss: 0.2507\n",
      "  Epoch: 07, Loss: 0.2523\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 14%|████████████▎                                                                           | 7/50 [33:03<3:23:16, 283.64s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 07, Loss: 0.2908\n",
      "  Epoch: 08, Loss: 0.2377\n",
      "  Epoch: 08, Loss: 0.2549\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 16%|██████████████                                                                          | 8/50 [37:47<3:18:35, 283.69s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 08, Loss: 0.3267\n",
      "  Epoch: 09, Loss: 0.2347\n",
      "  Epoch: 09, Loss: 0.2646\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 18%|███████████████▊                                                                        | 9/50 [42:31<3:13:55, 283.80s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 09, Loss: 0.3288\n",
      "  Epoch: 10, Loss: 0.2385\n",
      "  Epoch: 10, Loss: 0.2505\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 20%|█████████████████▍                                                                     | 10/50 [47:15<3:09:15, 283.89s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 10, Loss: 0.3213\n",
      "  Epoch: 11, Loss: 0.2322\n",
      "  Epoch: 11, Loss: 0.2463\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 22%|███████████████████▏                                                                   | 11/50 [51:59<3:04:27, 283.79s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 11, Loss: 0.3076\n",
      "  Epoch: 12, Loss: 0.2325\n",
      "  Epoch: 12, Loss: 0.2468\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 24%|████████████████████▉                                                                  | 12/50 [56:42<2:59:39, 283.67s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 12, Loss: 0.3185\n",
      "  Epoch: 13, Loss: 0.2588\n",
      "  Epoch: 13, Loss: 0.2479\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 26%|██████████████████████                                                               | 13/50 [1:01:26<2:54:54, 283.65s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 13, Loss: 0.3255\n",
      "  Epoch: 14, Loss: 0.2351\n",
      "  Epoch: 14, Loss: 0.2466\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 28%|███████████████████████▊                                                             | 14/50 [1:06:10<2:50:17, 283.81s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 14, Loss: 0.3251\n",
      "  Epoch: 15, Loss: 0.2302\n",
      "  Epoch: 15, Loss: 0.2417\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 30%|█████████████████████████▌                                                           | 15/50 [1:10:54<2:45:37, 283.93s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 15, Loss: 0.3215\n",
      "  Epoch: 16, Loss: 0.2324\n",
      "  Epoch: 16, Loss: 0.2404\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 32%|███████████████████████████▏                                                         | 16/50 [1:15:38<2:40:50, 283.84s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 16, Loss: 0.3173\n",
      "  Epoch: 17, Loss: 0.2327\n",
      "  Epoch: 17, Loss: 0.2473\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 34%|████████████████████████████▉                                                        | 17/50 [1:20:21<2:36:02, 283.71s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 17, Loss: 0.3179\n",
      "  Epoch: 18, Loss: 0.2408\n",
      "  Epoch: 18, Loss: 0.2457\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 36%|██████████████████████████████▌                                                      | 18/50 [1:25:05<2:31:21, 283.79s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 18, Loss: 0.2947\n",
      "  Epoch: 19, Loss: 0.2318\n",
      "  Epoch: 19, Loss: 0.2482\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 38%|████████████████████████████████▎                                                    | 19/50 [1:29:49<2:26:38, 283.81s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 19, Loss: 0.3221\n",
      "  Epoch: 20, Loss: 0.2666\n",
      "  Epoch: 20, Loss: 0.2578\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 40%|██████████████████████████████████                                                   | 20/50 [1:34:33<2:21:52, 283.75s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 20, Loss: 0.3059\n",
      "  Epoch: 21, Loss: 0.2286\n",
      "  Epoch: 21, Loss: 0.2435\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 42%|███████████████████████████████████▋                                                 | 21/50 [1:39:17<2:17:10, 283.79s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 21, Loss: 0.3120\n",
      "  Epoch: 22, Loss: 0.2301\n",
      "  Epoch: 22, Loss: 0.2421\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 44%|█████████████████████████████████████▍                                               | 22/50 [1:44:00<2:12:24, 283.73s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 22, Loss: 0.3217\n",
      "  Epoch: 23, Loss: 0.2343\n",
      "  Epoch: 23, Loss: 0.2432\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 46%|███████████████████████████████████████                                              | 23/50 [1:48:44<2:07:40, 283.73s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 23, Loss: 0.3044\n",
      "  Epoch: 24, Loss: 0.2275\n",
      "  Epoch: 24, Loss: 0.2429\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 48%|████████████████████████████████████████▊                                            | 24/50 [1:53:28<2:02:58, 283.78s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 24, Loss: 0.3091\n",
      "  Epoch: 25, Loss: 0.2297\n",
      "  Epoch: 25, Loss: 0.2418\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 50%|██████████████████████████████████████████▌                                          | 25/50 [1:58:11<1:58:09, 283.56s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 25, Loss: 0.3223\n",
      "  Epoch: 26, Loss: 0.2339\n",
      "  Epoch: 26, Loss: 0.2400\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 52%|████████████████████████████████████████████▏                                        | 26/50 [2:02:55<1:53:26, 283.62s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 26, Loss: 0.3149\n",
      "  Epoch: 27, Loss: 0.2307\n",
      "  Epoch: 27, Loss: 0.2418\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 54%|█████████████████████████████████████████████▉                                       | 27/50 [2:07:38<1:48:43, 283.63s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 27, Loss: 0.3231\n",
      "  Epoch: 28, Loss: 0.2306\n",
      "  Epoch: 28, Loss: 0.2376\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 56%|███████████████████████████████████████████████▌                                     | 28/50 [2:12:22<1:43:59, 283.60s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 28, Loss: 0.3224\n",
      "  Epoch: 29, Loss: 0.2331\n",
      "  Epoch: 29, Loss: 0.2520\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 58%|█████████████████████████████████████████████████▎                                   | 29/50 [2:17:06<1:39:18, 283.73s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 29, Loss: 0.3049\n",
      "  Epoch: 30, Loss: 0.2307\n",
      "  Epoch: 30, Loss: 0.2390\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 60%|███████████████████████████████████████████████████                                  | 30/50 [2:21:50<1:34:36, 283.82s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 30, Loss: 0.2687\n",
      "  Epoch: 31, Loss: 0.2408\n",
      "  Epoch: 31, Loss: 0.2404\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 62%|████████████████████████████████████████████████████▋                                | 31/50 [2:26:34<1:29:52, 283.82s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 31, Loss: 0.3142\n",
      "  Epoch: 32, Loss: 0.2484\n",
      "  Epoch: 32, Loss: 0.2356\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 64%|██████████████████████████████████████████████████████▍                              | 32/50 [2:31:17<1:25:05, 283.65s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 32, Loss: 0.3117\n",
      "  Epoch: 33, Loss: 0.2260\n",
      "  Epoch: 33, Loss: 0.2408\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 66%|████████████████████████████████████████████████████████                             | 33/50 [2:36:01<1:20:22, 283.69s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 33, Loss: 0.3192\n",
      "  Epoch: 34, Loss: 0.2286\n",
      "  Epoch: 34, Loss: 0.2406\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 68%|█████████████████████████████████████████████████████████▊                           | 34/50 [2:40:44<1:15:37, 283.62s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 34, Loss: 0.3079\n",
      "  Epoch: 35, Loss: 0.2269\n",
      "  Epoch: 35, Loss: 0.2464\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 70%|███████████████████████████████████████████████████████████▍                         | 35/50 [2:45:27<1:10:52, 283.51s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 35, Loss: 0.2719\n",
      "  Epoch: 36, Loss: 0.2270\n",
      "  Epoch: 36, Loss: 0.2360\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 72%|█████████████████████████████████████████████████████████████▏                       | 36/50 [2:50:11<1:06:09, 283.53s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 36, Loss: 0.3226\n",
      "  Epoch: 37, Loss: 0.2275\n",
      "  Epoch: 37, Loss: 0.2577\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 74%|██████████████████████████████████████████████████████████████▉                      | 37/50 [2:54:55<1:01:26, 283.60s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 37, Loss: 0.3051\n",
      "  Epoch: 38, Loss: 0.2269\n",
      "  Epoch: 38, Loss: 0.2442\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 76%|██████████████████████████████████████████████████████████████████                     | 38/50 [2:59:38<56:43, 283.64s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 38, Loss: 0.2714\n",
      "  Epoch: 39, Loss: 0.2340\n",
      "  Epoch: 39, Loss: 0.2397\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 78%|███████████████████████████████████████████████████████████████████▊                   | 39/50 [3:04:22<51:58, 283.54s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 39, Loss: 0.3179\n",
      "  Epoch: 40, Loss: 0.2275\n",
      "  Epoch: 40, Loss: 0.2388\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 80%|█████████████████████████████████████████████████████████████████████▌                 | 40/50 [3:09:05<47:14, 283.45s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 40, Loss: 0.2749\n",
      "  Epoch: 41, Loss: 0.2267\n",
      "  Epoch: 41, Loss: 0.2415\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 82%|███████████████████████████████████████████████████████████████████████▎               | 41/50 [3:13:48<42:31, 283.47s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 41, Loss: 0.3304\n",
      "  Epoch: 42, Loss: 0.2399\n",
      "  Epoch: 42, Loss: 0.2410\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 84%|█████████████████████████████████████████████████████████████████████████              | 42/50 [3:18:32<37:48, 283.51s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 42, Loss: 0.3077\n",
      "  Epoch: 43, Loss: 0.2455\n",
      "  Epoch: 43, Loss: 0.2434\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 86%|██████████████████████████████████████████████████████████████████████████▊            | 43/50 [3:23:16<33:04, 283.49s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 43, Loss: 0.3092\n",
      "  Epoch: 44, Loss: 0.2308\n",
      "  Epoch: 44, Loss: 0.2399\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 88%|████████████████████████████████████████████████████████████████████████████▌          | 44/50 [3:27:59<28:20, 283.43s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 44, Loss: 0.3094\n",
      "  Epoch: 45, Loss: 0.2264\n",
      "  Epoch: 45, Loss: 0.2500\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 90%|██████████████████████████████████████████████████████████████████████████████▎        | 45/50 [3:32:43<23:37, 283.52s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 45, Loss: 0.3107\n",
      "  Epoch: 46, Loss: 0.2284\n",
      "  Epoch: 46, Loss: 0.2406\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 92%|████████████████████████████████████████████████████████████████████████████████       | 46/50 [3:37:26<18:54, 283.52s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 46, Loss: 0.3197\n",
      "  Epoch: 47, Loss: 0.2294\n",
      "  Epoch: 47, Loss: 0.2421\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 94%|█████████████████████████████████████████████████████████████████████████████████▊     | 47/50 [3:42:09<14:10, 283.48s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 47, Loss: 0.3134\n",
      "  Epoch: 48, Loss: 0.2276\n",
      "  Epoch: 48, Loss: 0.2437\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 96%|███████████████████████████████████████████████████████████████████████████████████▌   | 48/50 [3:46:53<09:26, 283.49s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 48, Loss: 0.3096\n",
      "  Epoch: 49, Loss: 0.2319\n",
      "  Epoch: 49, Loss: 0.2567\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 98%|█████████████████████████████████████████████████████████████████████████████████████▎ | 49/50 [3:51:37<04:43, 283.59s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 49, Loss: 0.3105\n",
      "  Epoch: 50, Loss: 0.2389\n",
      "  Epoch: 50, Loss: 0.2377\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████| 50/50 [3:56:21<00:00, 283.62s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Epoch: 50, Loss: 0.3097\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "train_graphs=[\n",
    "    graph_4_3,\n",
    "    graph_4_4, \n",
    "    graph_4_5,\n",
    "#     graph_4_9\n",
    "]\n",
    "\n",
    "for epoch in tqdm(range(1, 51)):\n",
    "    for g in train_graphs:\n",
    "        loss = train(g)\n",
    "        print(f'  Epoch: {epoch:02d}, Loss: {loss:.4f}')\n",
    "#     scheduler.step()\n",
    "\n",
    "model=[memory,gnn, link_pred,neighbor_loader]\n",
    "os.system(\"mkdir -p ./models/\")\n",
    "torch.save(model,\"./models/model_saved_emb100_BATCH_1024_LastAggregator_multiclass_without_neg_edge.pt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time \n",
    "\n",
    "@torch.no_grad()\n",
    "def test_day_new(inference_data,path):\n",
    "    if os.path.exists(path):\n",
    "        pass\n",
    "    else:\n",
    "        os.mkdir(path)\n",
    "    \n",
    "#     m=torch.load(\"model_saved_emb100.pt\")\n",
    "#     memory,gnn, link_pred,neighbor_loader=m\n",
    "    memory.eval()\n",
    "    gnn.eval()\n",
    "    link_pred.eval()\n",
    "    \n",
    "    memory.reset_state()  # Start with a fresh memory.  \n",
    "    neighbor_loader.reset_state()  # Start with an empty graph.\n",
    "    \n",
    "    time_with_loss={}\n",
    "    total_loss = 0    \n",
    "    edge_list=[]\n",
    "    \n",
    "    unique_nodes=torch.tensor([]).to(device=device)\n",
    "    total_edges=0\n",
    "    \n",
    "#     test_memory=copy.deepcopy(memory)   \n",
    "#     test_gnn=copy.deepcopy(gnn)   \n",
    "#     test_link_pred=copy.deepcopy(link_pred) \n",
    "#     test_neighbor_loader=copy.deepcopy(neighbor_loader)\n",
    "\n",
    "\n",
    "\n",
    "    start_time=inference_data.t[0]\n",
    "    event_count=0\n",
    "    \n",
    "    pos_o=[]\n",
    "    \n",
    "    loss_list=[]\n",
    "    \n",
    "#     print(\"before merge:\",train_data)\n",
    "\n",
    "#     nique_node_count=len(torch.cat([train_data.src,train_data.dst]).unique())\n",
    "\n",
    "    print(\"after merge:\",inference_data)\n",
    "    \n",
    "    # Record the running time to evaluate the performance\n",
    "    start = time.perf_counter()\n",
    "\n",
    "    for batch in inference_data.seq_batches(batch_size=BATCH):\n",
    "        \n",
    "        src, pos_dst, t, msg = batch.src, batch.dst, batch.t, batch.msg\n",
    "        unique_nodes=torch.cat([unique_nodes,src,pos_dst]).unique()\n",
    "        total_edges+=BATCH\n",
    "        \n",
    "       \n",
    "        n_id = torch.cat([src, pos_dst]).unique()       \n",
    "        n_id, edge_index, e_id = neighbor_loader(n_id)\n",
    "        assoc[n_id] = torch.arange(n_id.size(0), device=device)\n",
    "\n",
    "        z, last_update = memory(n_id)\n",
    "        z = gnn(z, last_update, edge_index, inference_data.t[e_id], inference_data.msg[e_id])\n",
    "\n",
    "        pos_out = link_pred(z[assoc[src]], z[assoc[pos_dst]])\n",
    "        \n",
    "        pos_o.append(pos_out)\n",
    "        y_pred = torch.cat([pos_out], dim=0)\n",
    "#         y_true = torch.cat(\n",
    "#             [torch.ones(pos_out.size(0))], dim=0).to(torch.long)     \n",
    "#         y_true=y_true.reshape(-1).to(torch.long)\n",
    "\n",
    "        y_true=[]\n",
    "        for m in msg:\n",
    "            l=tensor_find(m[16:-16],1)-1\n",
    "            y_true.append(l) \n",
    "        y_true = torch.tensor(y_true)\n",
    "        y_true=y_true.reshape(-1).to(torch.long).to(device=device)\n",
    "\n",
    "        # Only consider which edge hasn't been correctly predicted.\n",
    "        # For benign graphs, the behaviors patterns are similar and therefore their losses are small\n",
    "        # For anoamlous behaviors, some behaviors might not be seen before, so the probability of predicting those edges are low. Thus their losses are high.\n",
    "        loss = criterion(y_pred, y_true)\n",
    "\n",
    "        total_loss += float(loss) * batch.num_events\n",
    "     \n",
    "        \n",
    "        # update the edges in the batch to the memory and neighbor_loader\n",
    "        memory.update_state(src, pos_dst, t, msg)\n",
    "        neighbor_loader.insert(src, pos_dst)\n",
    "        \n",
    "        # compute the loss for each edge\n",
    "        each_edge_loss= cal_pos_edges_loss_multiclass(pos_out,y_true)\n",
    "        \n",
    "        for i in range(len(pos_out)):\n",
    "            srcnode=int(src[i])\n",
    "            dstnode=int(pos_dst[i])  \n",
    "            \n",
    "            srcmsg=str(nodeid2msg[srcnode]) \n",
    "            dstmsg=str(nodeid2msg[dstnode])\n",
    "            t_var=int(t[i])\n",
    "            edgeindex=tensor_find(msg[i][16:-16],1)    \n",
    "            edge_type=rel2id[edgeindex]\n",
    "            loss=each_edge_loss[i]    \n",
    "\n",
    "            temp_dic={}\n",
    "            temp_dic['loss']=float(loss)\n",
    "            temp_dic['srcnode']=srcnode\n",
    "            temp_dic['dstnode']=dstnode\n",
    "            temp_dic['srcmsg']=srcmsg\n",
    "            temp_dic['dstmsg']=dstmsg\n",
    "            temp_dic['edge_type']=edge_type\n",
    "            temp_dic['time']=t_var\n",
    "            \n",
    "\n",
    "#             if \"netflow\" in srcmsg or \"netflow\" in dstmsg:\n",
    "#                 temp_dic['loss']=0\n",
    "            edge_list.append(temp_dic)\n",
    "        \n",
    "        event_count+=len(batch.src)\n",
    "        if t[-1]>start_time+60000000000*15:\n",
    "            # Here is a checkpoint, which records all edge losses in the current time window\n",
    "#             loss=total_loss/event_count\n",
    "            time_interval=ns_time_to_datetime_US(start_time)+\"~\"+ns_time_to_datetime_US(t[-1])\n",
    "\n",
    "            end = time.perf_counter()\n",
    "            time_with_loss[time_interval]={'loss':loss,\n",
    "                                \n",
    "                                          'nodes_count':len(unique_nodes),\n",
    "                                          'total_edges':total_edges,\n",
    "                                          'costed_time':(end-start)}\n",
    "            \n",
    "            \n",
    "            log=open(path+\"/\"+time_interval+\".txt\",'w')\n",
    "            \n",
    "            for e in edge_list: \n",
    "#                 temp_key=e['srcmsg']+e['dstmsg']+e['edge_type']\n",
    "#                 if temp_key in train_edge_set:      \n",
    "# #                     e['loss']=(e['loss']-train_edge_set[temp_key]) if e['loss']>=train_edge_set[temp_key] else 0  \n",
    "# #                     e['loss']=abs(e['loss']-train_edge_set[temp_key])\n",
    "                    \n",
    "#                     e['modified']=True\n",
    "#                 else:\n",
    "#                     e['modified']=False\n",
    "                loss+=e['loss']\n",
    "\n",
    "            loss=loss/event_count   \n",
    "            print(f'Time: {time_interval}, Loss: {loss:.4f}, Nodes_count: {len(unique_nodes)}, Cost Time: {(end-start):.2f}s')\n",
    "            edge_list = sorted(edge_list, key=lambda x:x['loss'],reverse=True)   # Rank the results based on edge loss\n",
    "            for e in edge_list: \n",
    "                log.write(str(e))\n",
    "                log.write(\"\\n\") \n",
    "            event_count=0\n",
    "            total_loss=0\n",
    "            loss=0\n",
    "            start_time=t[-1]\n",
    "            log.close()\n",
    "            edge_list.clear()\n",
    "            \n",
    " \n",
    "    return time_with_loss\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test 4-9 ~ 4-12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after merge: TemporalData(dst=[8230837], msg=[8230837, 41], src=[8230837], t=[8230837])\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yinyuanl/anaconda3/envs/kairos/lib/python3.9/site-packages/torch_geometric/nn/conv/transformer_conv.py:211: UserWarning: operator() profile_node %28 : int[] = prim::profile_ivalue(%size.4)\n",
      " does not have profile information (Triggered internally at /opt/conda/conda-bld/pytorch_1670525539683/work/torch/csrc/jit/codegen/cuda/graph_fuser.cpp:105.)\n",
      "  alpha = softmax(alpha, index, ptr, size_i)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time: 2018-04-03 10:02:14.348882872~2018-04-03 10:17:22.845633004, Loss: 0.6345, Nodes_count: 2021, Cost Time: 1.75s\n",
      "Time: 2018-04-03 10:17:22.845633004~2018-04-03 10:32:52.909926097, Loss: 0.4312, Nodes_count: 5070, Cost Time: 6.40s\n",
      "Time: 2018-04-03 10:32:52.909926097~2018-04-03 10:47:54.761593566, Loss: 0.2277, Nodes_count: 9219, Cost Time: 14.46s\n",
      "Time: 2018-04-03 10:47:54.761593566~2018-04-03 11:03:00.878438338, Loss: 0.2211, Nodes_count: 13173, Cost Time: 22.21s\n",
      "Time: 2018-04-03 11:03:00.878438338~2018-04-03 11:18:31.001956600, Loss: 0.2544, Nodes_count: 17464, Cost Time: 32.41s\n",
      "Time: 2018-04-03 11:18:31.001956600~2018-04-03 11:33:31.167232690, Loss: 0.4785, Nodes_count: 20766, Cost Time: 44.63s\n",
      "Time: 2018-04-03 11:33:31.167232690~2018-04-03 11:48:41.419633970, Loss: 0.2333, Nodes_count: 24304, Cost Time: 55.46s\n",
      "Time: 2018-04-03 11:48:41.419633970~2018-04-03 12:03:45.030247497, Loss: 0.2248, Nodes_count: 28293, Cost Time: 65.26s\n",
      "Time: 2018-04-03 12:03:45.030247497~2018-04-03 12:18:47.839285700, Loss: 0.4896, Nodes_count: 31666, Cost Time: 76.00s\n",
      "Time: 2018-04-03 12:18:47.839285700~2018-04-03 12:33:49.967236590, Loss: 0.2969, Nodes_count: 35433, Cost Time: 86.01s\n",
      "Time: 2018-04-03 12:33:49.967236590~2018-04-03 12:48:57.259651226, Loss: 0.8584, Nodes_count: 38862, Cost Time: 99.58s\n",
      "Time: 2018-04-03 12:48:57.259651226~2018-04-03 13:04:02.392807652, Loss: 0.4913, Nodes_count: 43813, Cost Time: 113.20s\n",
      "Time: 2018-04-03 13:04:02.392807652~2018-04-03 13:19:03.028948702, Loss: 0.2797, Nodes_count: 47121, Cost Time: 123.43s\n",
      "Time: 2018-04-03 13:19:03.028948702~2018-04-03 13:34:13.317544873, Loss: 0.2711, Nodes_count: 51294, Cost Time: 132.62s\n",
      "Time: 2018-04-03 13:34:13.317544873~2018-04-03 13:49:19.611788042, Loss: 0.2899, Nodes_count: 55377, Cost Time: 143.92s\n",
      "Time: 2018-04-03 13:49:19.611788042~2018-04-03 14:04:20.148525030, Loss: 0.2659, Nodes_count: 59805, Cost Time: 155.01s\n",
      "Time: 2018-04-03 14:04:20.148525030~2018-04-03 14:19:42.950320544, Loss: 0.3146, Nodes_count: 63717, Cost Time: 166.93s\n",
      "Time: 2018-04-03 14:19:42.950320544~2018-04-03 14:34:55.468560096, Loss: 0.2460, Nodes_count: 66836, Cost Time: 176.80s\n",
      "Time: 2018-04-03 14:34:55.468560096~2018-04-03 14:49:55.906975533, Loss: 0.2628, Nodes_count: 70047, Cost Time: 186.29s\n",
      "Time: 2018-04-03 14:49:55.906975533~2018-04-03 15:05:17.225752446, Loss: 0.5279, Nodes_count: 73707, Cost Time: 197.55s\n",
      "Time: 2018-04-03 15:05:17.225752446~2018-04-03 15:20:42.905251499, Loss: 0.4592, Nodes_count: 77224, Cost Time: 210.73s\n",
      "Time: 2018-04-03 15:20:42.905251499~2018-04-03 15:35:49.550464356, Loss: 0.3009, Nodes_count: 81483, Cost Time: 221.79s\n",
      "Time: 2018-04-03 15:35:49.550464356~2018-04-03 15:50:51.296026154, Loss: 0.2847, Nodes_count: 85100, Cost Time: 231.75s\n",
      "Time: 2018-04-03 15:50:51.296026154~2018-04-03 16:06:03.009765083, Loss: 0.3597, Nodes_count: 88139, Cost Time: 242.21s\n",
      "Time: 2018-04-03 16:06:03.009765083~2018-04-03 16:21:09.934244896, Loss: 0.8892, Nodes_count: 90994, Cost Time: 256.34s\n",
      "Time: 2018-04-03 16:21:09.934244896~2018-04-03 16:36:30.966874938, Loss: 0.2466, Nodes_count: 94152, Cost Time: 267.28s\n",
      "Time: 2018-04-03 16:36:30.966874938~2018-04-03 16:51:32.691970221, Loss: 0.2718, Nodes_count: 97688, Cost Time: 277.32s\n",
      "Time: 2018-04-03 16:51:32.691970221~2018-04-03 17:06:39.950286941, Loss: 0.3755, Nodes_count: 100789, Cost Time: 288.79s\n",
      "Time: 2018-04-03 17:06:39.950286941~2018-04-03 17:21:49.899077955, Loss: 0.2528, Nodes_count: 104248, Cost Time: 299.65s\n",
      "Time: 2018-04-03 17:21:49.899077955~2018-04-03 17:36:50.410308261, Loss: 0.2583, Nodes_count: 108851, Cost Time: 311.46s\n",
      "Time: 2018-04-03 17:36:50.410308261~2018-04-03 17:52:10.093292239, Loss: 0.2815, Nodes_count: 112662, Cost Time: 323.72s\n",
      "Time: 2018-04-03 17:52:10.093292239~2018-04-03 18:07:21.284264050, Loss: 0.2288, Nodes_count: 115505, Cost Time: 336.92s\n",
      "Time: 2018-04-03 18:07:21.284264050~2018-04-03 18:22:30.495299312, Loss: 0.1970, Nodes_count: 118394, Cost Time: 348.94s\n",
      "Time: 2018-04-03 18:22:30.495299312~2018-04-03 18:37:31.061410984, Loss: 0.2279, Nodes_count: 120909, Cost Time: 358.65s\n",
      "Time: 2018-04-03 18:37:31.061410984~2018-04-03 18:52:40.844261199, Loss: 0.3297, Nodes_count: 123985, Cost Time: 369.68s\n",
      "Time: 2018-04-03 18:52:40.844261199~2018-04-03 19:07:57.268552572, Loss: 0.2955, Nodes_count: 126778, Cost Time: 381.04s\n",
      "Time: 2018-04-03 19:07:57.268552572~2018-04-03 19:22:57.667409530, Loss: 0.2501, Nodes_count: 129760, Cost Time: 391.41s\n",
      "Time: 2018-04-03 19:22:57.667409530~2018-04-03 19:38:03.128381928, Loss: 0.3770, Nodes_count: 132499, Cost Time: 403.26s\n",
      "Time: 2018-04-03 19:38:03.128381928~2018-04-03 19:53:03.583854915, Loss: 0.4027, Nodes_count: 135527, Cost Time: 415.44s\n",
      "Time: 2018-04-03 19:53:03.583854915~2018-04-03 20:08:10.760419548, Loss: 0.2743, Nodes_count: 138362, Cost Time: 425.47s\n",
      "Time: 2018-04-03 20:08:10.760419548~2018-04-03 20:23:14.002015733, Loss: 0.5116, Nodes_count: 142116, Cost Time: 438.49s\n",
      "Time: 2018-04-03 20:23:14.002015733~2018-04-03 20:38:14.205988634, Loss: 0.7835, Nodes_count: 144578, Cost Time: 451.60s\n",
      "Time: 2018-04-03 20:38:14.205988634~2018-04-03 20:53:16.509329728, Loss: 0.8234, Nodes_count: 146954, Cost Time: 463.99s\n",
      "Time: 2018-04-03 20:53:16.509329728~2018-04-03 21:08:25.472358311, Loss: 0.5701, Nodes_count: 149567, Cost Time: 475.35s\n",
      "Time: 2018-04-03 21:08:25.472358311~2018-04-03 21:23:30.301281990, Loss: 0.2843, Nodes_count: 152654, Cost Time: 485.94s\n",
      "Time: 2018-04-03 21:23:30.301281990~2018-04-03 21:38:43.348432077, Loss: 0.2923, Nodes_count: 155749, Cost Time: 496.00s\n",
      "Time: 2018-04-03 21:38:43.348432077~2018-04-03 21:54:48.906986337, Loss: 0.2215, Nodes_count: 155951, Cost Time: 497.33s\n",
      "Time: 2018-04-03 21:54:48.906986337~2018-04-03 22:10:59.601975592, Loss: 0.1827, Nodes_count: 156049, Cost Time: 498.01s\n",
      "Time: 2018-04-03 22:10:59.601975592~2018-04-03 22:26:59.945565650, Loss: 0.1819, Nodes_count: 156142, Cost Time: 498.68s\n",
      "Time: 2018-04-03 22:26:59.945565650~2018-04-03 22:42:00.260283916, Loss: 0.1805, Nodes_count: 156227, Cost Time: 499.29s\n",
      "Time: 2018-04-03 22:42:00.260283916~2018-04-03 22:58:00.583020420, Loss: 0.1835, Nodes_count: 156316, Cost Time: 499.96s\n",
      "Time: 2018-04-03 22:58:00.583020420~2018-04-03 23:14:17.840327468, Loss: 0.1822, Nodes_count: 156414, Cost Time: 500.64s\n",
      "Time: 2018-04-03 23:14:17.840327468~2018-04-03 23:30:09.875566664, Loss: 0.1905, Nodes_count: 156506, Cost Time: 501.31s\n",
      "Time: 2018-04-03 23:30:09.875566664~2018-04-03 23:46:37.900106125, Loss: 0.1859, Nodes_count: 156598, Cost Time: 501.98s\n"
     ]
    }
   ],
   "source": [
    "model=torch.load(\"./models/model_saved_emb100_BATCH_1024_LastAggregator_multiclass_without_neg_edge.pt\",map_location=device)\n",
    "memory,gnn, link_pred,neighbor_loader=model\n",
    "ans_4_3=test_day_new(graph_4_3,\"graph_4_3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after merge: TemporalData(dst=[4930304], msg=[4930304, 41], src=[4930304], t=[4930304])\n",
      "Time: 2018-04-04 00:00:00.001798512~2018-04-04 00:15:02.197293670, Loss: 0.5028, Nodes_count: 363, Cost Time: 0.70s\n",
      "Time: 2018-04-04 00:15:02.197293670~2018-04-04 00:30:28.978098397, Loss: 0.2540, Nodes_count: 658, Cost Time: 1.43s\n",
      "Time: 2018-04-04 00:30:28.978098397~2018-04-04 00:45:32.001547272, Loss: 0.2739, Nodes_count: 987, Cost Time: 2.16s\n",
      "Time: 2018-04-04 00:45:32.001547272~2018-04-04 01:01:12.511397075, Loss: 0.3430, Nodes_count: 1228, Cost Time: 2.88s\n",
      "Time: 2018-04-04 01:01:12.511397075~2018-04-04 01:16:50.001162895, Loss: 0.3142, Nodes_count: 1488, Cost Time: 3.61s\n",
      "Time: 2018-04-04 01:16:50.001162895~2018-04-04 01:32:05.001431940, Loss: 0.2898, Nodes_count: 1751, Cost Time: 4.34s\n",
      "Time: 2018-04-04 01:32:05.001431940~2018-04-04 01:47:27.933058106, Loss: 0.2804, Nodes_count: 2030, Cost Time: 5.07s\n",
      "Time: 2018-04-04 01:47:27.933058106~2018-04-04 02:02:58.001409515, Loss: 0.2490, Nodes_count: 2285, Cost Time: 5.80s\n",
      "Time: 2018-04-04 02:02:58.001409515~2018-04-04 02:18:16.002083795, Loss: 0.2623, Nodes_count: 2538, Cost Time: 6.53s\n",
      "Time: 2018-04-04 02:18:16.002083795~2018-04-04 02:34:05.117133144, Loss: 0.2423, Nodes_count: 2772, Cost Time: 7.27s\n",
      "Time: 2018-04-04 02:34:05.117133144~2018-04-04 02:49:12.001171940, Loss: 0.2681, Nodes_count: 3047, Cost Time: 8.00s\n",
      "Time: 2018-04-04 02:49:12.001171940~2018-04-04 03:04:39.001274152, Loss: 0.2567, Nodes_count: 3300, Cost Time: 8.95s\n",
      "Time: 2018-04-04 03:04:39.001274152~2018-04-04 03:20:03.015715516, Loss: 0.2468, Nodes_count: 3549, Cost Time: 9.68s\n",
      "Time: 2018-04-04 03:20:03.015715516~2018-04-04 03:35:18.002304214, Loss: 0.2462, Nodes_count: 3812, Cost Time: 10.41s\n",
      "Time: 2018-04-04 03:35:18.002304214~2018-04-04 03:50:43.001174904, Loss: 0.2444, Nodes_count: 4066, Cost Time: 11.14s\n",
      "Time: 2018-04-04 03:50:43.001174904~2018-04-04 04:06:07.121472553, Loss: 0.2464, Nodes_count: 4313, Cost Time: 11.87s\n",
      "Time: 2018-04-04 04:06:07.121472553~2018-04-04 04:21:16.655120668, Loss: 0.2756, Nodes_count: 4569, Cost Time: 12.60s\n",
      "Time: 2018-04-04 04:21:16.655120668~2018-04-04 04:36:40.001160845, Loss: 0.2558, Nodes_count: 4819, Cost Time: 13.33s\n",
      "Time: 2018-04-04 04:36:40.001160845~2018-04-04 04:52:08.119574735, Loss: 0.2486, Nodes_count: 5056, Cost Time: 14.11s\n",
      "Time: 2018-04-04 04:52:08.119574735~2018-04-04 05:07:30.001026160, Loss: 0.2423, Nodes_count: 5310, Cost Time: 14.84s\n",
      "Time: 2018-04-04 05:07:30.001026160~2018-04-04 05:23:40.001345904, Loss: 0.2505, Nodes_count: 5587, Cost Time: 15.62s\n",
      "Time: 2018-04-04 05:23:40.001345904~2018-04-04 05:39:23.002297477, Loss: 0.2378, Nodes_count: 5806, Cost Time: 16.35s\n",
      "Time: 2018-04-04 05:39:23.002297477~2018-04-04 05:54:35.056350190, Loss: 0.2548, Nodes_count: 6055, Cost Time: 17.08s\n",
      "Time: 2018-04-04 05:54:35.056350190~2018-04-04 06:10:09.826466525, Loss: 0.2457, Nodes_count: 6294, Cost Time: 17.80s\n",
      "Time: 2018-04-04 06:10:09.826466525~2018-04-04 06:25:13.928789336, Loss: 0.2308, Nodes_count: 6523, Cost Time: 18.53s\n",
      "Time: 2018-04-04 06:25:13.928789336~2018-04-04 06:40:44.001404910, Loss: 0.2437, Nodes_count: 6753, Cost Time: 19.25s\n",
      "Time: 2018-04-04 06:40:44.001404910~2018-04-04 06:56:02.002474942, Loss: 0.2645, Nodes_count: 7008, Cost Time: 19.98s\n",
      "Time: 2018-04-04 06:56:02.002474942~2018-04-04 07:11:35.366078188, Loss: 0.2340, Nodes_count: 7231, Cost Time: 20.71s\n",
      "Time: 2018-04-04 07:11:35.366078188~2018-04-04 07:27:05.001809403, Loss: 0.2354, Nodes_count: 7447, Cost Time: 21.43s\n",
      "Time: 2018-04-04 07:27:05.001809403~2018-04-04 07:42:09.649507278, Loss: 1.0682, Nodes_count: 16551, Cost Time: 25.09s\n",
      "Time: 2018-04-04 07:42:09.649507278~2018-04-04 07:57:13.001812254, Loss: 1.1613, Nodes_count: 18133, Cost Time: 27.51s\n",
      "Time: 2018-04-04 07:57:13.001812254~2018-04-04 08:12:36.640740470, Loss: 0.2825, Nodes_count: 18366, Cost Time: 28.30s\n",
      "Time: 2018-04-04 08:12:36.640740470~2018-04-04 08:28:02.001871065, Loss: 0.3456, Nodes_count: 18595, Cost Time: 29.03s\n",
      "Time: 2018-04-04 08:28:02.001871065~2018-04-04 08:43:16.001597748, Loss: 0.3658, Nodes_count: 18818, Cost Time: 29.76s\n",
      "Time: 2018-04-04 08:43:16.001597748~2018-04-04 08:58:45.001299815, Loss: 0.2994, Nodes_count: 19027, Cost Time: 30.48s\n",
      "Time: 2018-04-04 08:58:45.001299815~2018-04-04 09:14:22.001694097, Loss: 0.4047, Nodes_count: 21983, Cost Time: 35.20s\n",
      "Time: 2018-04-04 09:14:22.001694097~2018-04-04 09:29:31.001807614, Loss: 0.2980, Nodes_count: 22729, Cost Time: 37.61s\n",
      "Time: 2018-04-04 09:29:31.001807614~2018-04-04 09:44:37.689944648, Loss: 0.3608, Nodes_count: 27073, Cost Time: 46.81s\n",
      "Time: 2018-04-04 09:44:37.689944648~2018-04-04 10:00:02.962616019, Loss: 0.3311, Nodes_count: 29306, Cost Time: 54.07s\n",
      "Time: 2018-04-04 10:00:02.962616019~2018-04-04 10:15:22.191561508, Loss: 0.3258, Nodes_count: 33438, Cost Time: 63.76s\n",
      "Time: 2018-04-04 10:15:22.191561508~2018-04-04 10:30:25.590258622, Loss: 0.2718, Nodes_count: 38473, Cost Time: 74.51s\n",
      "Time: 2018-04-04 10:30:25.590258622~2018-04-04 10:46:38.748764170, Loss: 0.2719, Nodes_count: 40851, Cost Time: 80.72s\n",
      "Time: 2018-04-04 10:46:38.748764170~2018-04-04 11:02:10.002388935, Loss: 0.2700, Nodes_count: 43595, Cost Time: 88.22s\n",
      "Time: 2018-04-04 11:02:10.002388935~2018-04-04 11:17:17.002056664, Loss: 0.2704, Nodes_count: 44603, Cost Time: 92.04s\n",
      "Time: 2018-04-04 11:17:17.002056664~2018-04-04 11:32:42.002563278, Loss: 0.2998, Nodes_count: 48591, Cost Time: 101.44s\n",
      "Time: 2018-04-04 11:32:42.002563278~2018-04-04 11:48:02.199361778, Loss: 0.3056, Nodes_count: 52482, Cost Time: 111.39s\n",
      "Time: 2018-04-04 11:48:02.199361778~2018-04-04 12:03:08.095300989, Loss: 0.2958, Nodes_count: 56116, Cost Time: 121.02s\n",
      "Time: 2018-04-04 12:03:08.095300989~2018-04-04 12:18:40.002516605, Loss: 0.3173, Nodes_count: 59270, Cost Time: 129.87s\n",
      "Time: 2018-04-04 12:18:40.002516605~2018-04-04 12:33:55.001602517, Loss: 0.2854, Nodes_count: 64184, Cost Time: 141.42s\n",
      "Time: 2018-04-04 12:33:55.001602517~2018-04-04 12:48:56.001107884, Loss: 0.3112, Nodes_count: 68503, Cost Time: 151.68s\n",
      "Time: 2018-04-04 12:48:56.001107884~2018-04-04 13:05:03.002399240, Loss: 0.3537, Nodes_count: 70738, Cost Time: 157.68s\n",
      "Time: 2018-04-04 13:05:03.002399240~2018-04-04 13:20:20.422248747, Loss: 0.3361, Nodes_count: 74914, Cost Time: 167.88s\n",
      "Time: 2018-04-04 13:20:20.422248747~2018-04-04 13:35:31.654897137, Loss: 0.3001, Nodes_count: 78094, Cost Time: 177.18s\n",
      "Time: 2018-04-04 13:35:31.654897137~2018-04-04 13:50:34.182425022, Loss: 0.3058, Nodes_count: 80801, Cost Time: 185.52s\n",
      "Time: 2018-04-04 13:50:34.182425022~2018-04-04 14:05:34.944980767, Loss: 0.3150, Nodes_count: 83836, Cost Time: 195.29s\n",
      "Time: 2018-04-04 14:05:34.944980767~2018-04-04 14:20:36.887870578, Loss: 0.3177, Nodes_count: 86631, Cost Time: 203.70s\n",
      "Time: 2018-04-04 14:20:36.887870578~2018-04-04 14:35:49.820999583, Loss: 0.2959, Nodes_count: 90323, Cost Time: 213.82s\n",
      "Time: 2018-04-04 14:35:49.820999583~2018-04-04 14:50:57.187772380, Loss: 0.2989, Nodes_count: 93403, Cost Time: 224.18s\n",
      "Time: 2018-04-04 14:50:57.187772380~2018-04-04 15:06:01.001693281, Loss: 0.2945, Nodes_count: 96537, Cost Time: 233.93s\n",
      "Time: 2018-04-04 15:06:01.001693281~2018-04-04 15:21:03.019481634, Loss: 0.3236, Nodes_count: 99507, Cost Time: 243.91s\n",
      "Time: 2018-04-04 15:21:03.019481634~2018-04-04 15:36:17.939548591, Loss: 0.3208, Nodes_count: 102633, Cost Time: 254.50s\n",
      "Time: 2018-04-04 15:36:17.939548591~2018-04-04 15:51:27.002313144, Loss: 0.2851, Nodes_count: 104409, Cost Time: 264.37s\n",
      "Time: 2018-04-04 15:51:27.002313144~2018-04-04 16:07:37.001276551, Loss: 0.2761, Nodes_count: 104895, Cost Time: 266.72s\n",
      "Time: 2018-04-04 16:07:37.001276551~2018-04-04 16:22:46.152460752, Loss: 0.1853, Nodes_count: 105000, Cost Time: 267.51s\n",
      "Time: 2018-04-04 16:22:46.152460752~2018-04-04 16:38:09.877581945, Loss: 0.1888, Nodes_count: 105104, Cost Time: 268.25s\n",
      "Time: 2018-04-04 16:38:09.877581945~2018-04-04 16:54:28.132530420, Loss: 0.1339, Nodes_count: 105600, Cost Time: 275.65s\n",
      "Time: 2018-04-04 16:54:28.132530420~2018-04-04 17:10:36.002552430, Loss: 0.1675, Nodes_count: 106381, Cost Time: 280.67s\n",
      "Time: 2018-04-04 17:10:36.002552430~2018-04-04 17:26:39.002198488, Loss: 0.2012, Nodes_count: 106485, Cost Time: 281.65s\n",
      "Time: 2018-04-04 17:26:39.002198488~2018-04-04 17:41:44.836673063, Loss: 0.2330, Nodes_count: 106582, Cost Time: 282.38s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time: 2018-04-04 17:41:44.836673063~2018-04-04 17:57:39.002224606, Loss: 0.2336, Nodes_count: 106686, Cost Time: 283.17s\n",
      "Time: 2018-04-04 17:57:39.002224606~2018-04-04 18:13:31.577322823, Loss: 0.2641, Nodes_count: 106796, Cost Time: 283.96s\n",
      "Time: 2018-04-04 18:13:31.577322823~2018-04-04 18:29:20.002185349, Loss: 0.2910, Nodes_count: 106905, Cost Time: 284.76s\n",
      "Time: 2018-04-04 18:29:20.002185349~2018-04-04 18:44:22.002253035, Loss: 0.3140, Nodes_count: 106997, Cost Time: 285.49s\n",
      "Time: 2018-04-04 18:44:22.002253035~2018-04-04 19:00:32.002193142, Loss: 0.2952, Nodes_count: 107108, Cost Time: 286.28s\n",
      "Time: 2018-04-04 19:00:32.002193142~2018-04-04 19:15:34.527395469, Loss: 0.2884, Nodes_count: 107204, Cost Time: 287.01s\n",
      "Time: 2018-04-04 19:15:34.527395469~2018-04-04 19:31:45.002510268, Loss: 0.2328, Nodes_count: 107302, Cost Time: 287.79s\n",
      "Time: 2018-04-04 19:31:45.002510268~2018-04-04 19:46:46.421253794, Loss: 0.2318, Nodes_count: 107387, Cost Time: 288.54s\n",
      "Time: 2018-04-04 19:46:46.421253794~2018-04-04 20:02:50.002457858, Loss: 0.2208, Nodes_count: 107489, Cost Time: 289.32s\n",
      "Time: 2018-04-04 20:02:50.002457858~2018-04-04 20:18:55.002429065, Loss: 0.2092, Nodes_count: 107594, Cost Time: 290.12s\n",
      "Time: 2018-04-04 20:18:55.002429065~2018-04-04 20:33:56.002192779, Loss: 0.2080, Nodes_count: 107676, Cost Time: 290.86s\n",
      "Time: 2018-04-04 20:33:56.002192779~2018-04-04 20:50:11.002177941, Loss: 0.1895, Nodes_count: 107778, Cost Time: 291.65s\n",
      "Time: 2018-04-04 20:50:11.002177941~2018-04-04 21:06:17.002207789, Loss: 0.1995, Nodes_count: 107884, Cost Time: 292.44s\n",
      "Time: 2018-04-04 21:06:17.002207789~2018-04-04 21:22:03.008085970, Loss: 0.2021, Nodes_count: 107989, Cost Time: 293.23s\n",
      "Time: 2018-04-04 21:22:03.008085970~2018-04-04 21:37:03.014949939, Loss: 0.1867, Nodes_count: 108086, Cost Time: 293.97s\n",
      "Time: 2018-04-04 21:37:03.014949939~2018-04-04 21:53:00.002472925, Loss: 0.2203, Nodes_count: 108193, Cost Time: 294.76s\n",
      "Time: 2018-04-04 21:53:00.002472925~2018-04-04 22:08:58.002227846, Loss: 0.2235, Nodes_count: 108295, Cost Time: 295.55s\n",
      "Time: 2018-04-04 22:08:58.002227846~2018-04-04 22:24:49.866594767, Loss: 0.1830, Nodes_count: 108397, Cost Time: 296.34s\n",
      "Time: 2018-04-04 22:24:49.866594767~2018-04-04 22:40:55.002033877, Loss: 0.1915, Nodes_count: 108496, Cost Time: 297.13s\n",
      "Time: 2018-04-04 22:40:55.002033877~2018-04-04 22:57:04.002525819, Loss: 0.1838, Nodes_count: 108585, Cost Time: 297.92s\n",
      "Time: 2018-04-04 22:57:04.002525819~2018-04-04 23:13:10.654906492, Loss: 0.1921, Nodes_count: 108688, Cost Time: 298.72s\n",
      "Time: 2018-04-04 23:13:10.654906492~2018-04-04 23:29:05.001705015, Loss: 0.1912, Nodes_count: 108792, Cost Time: 299.52s\n",
      "Time: 2018-04-04 23:29:05.001705015~2018-04-04 23:44:08.907479821, Loss: 0.2037, Nodes_count: 108884, Cost Time: 300.26s\n",
      "Time: 2018-04-04 23:44:08.907479821~2018-04-04 23:59:59.002570803, Loss: 0.2153, Nodes_count: 108996, Cost Time: 301.44s\n"
     ]
    }
   ],
   "source": [
    "model=torch.load(\"./models/model_saved_emb100_BATCH_1024_LastAggregator_multiclass_without_neg_edge.pt\",map_location=device)\n",
    "memory,gnn, link_pred,neighbor_loader=model\n",
    "ans_4_4=test_day_new(graph_4_4,\"graph_4_4\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after merge: TemporalData(dst=[1489011], msg=[1489011, 41], src=[1489011], t=[1489011])\n",
      "Time: 2018-04-05 00:00:00.001766694~2018-04-05 00:16:00.002476684, Loss: 0.5783, Nodes_count: 346, Cost Time: 0.65s\n",
      "Time: 2018-04-05 00:16:00.002476684~2018-04-05 00:31:52.143639968, Loss: 0.3052, Nodes_count: 650, Cost Time: 1.32s\n",
      "Time: 2018-04-05 00:31:52.143639968~2018-04-05 00:47:52.002321096, Loss: 0.2980, Nodes_count: 937, Cost Time: 2.00s\n",
      "Time: 2018-04-05 00:47:52.002321096~2018-04-05 01:03:35.001510674, Loss: 0.3421, Nodes_count: 1217, Cost Time: 2.68s\n",
      "Time: 2018-04-05 01:03:35.001510674~2018-04-05 01:19:09.002324977, Loss: 0.3718, Nodes_count: 1489, Cost Time: 3.35s\n",
      "Time: 2018-04-05 01:19:09.002324977~2018-04-05 01:35:25.002311363, Loss: 0.3583, Nodes_count: 1746, Cost Time: 4.03s\n",
      "Time: 2018-04-05 01:35:25.002311363~2018-04-05 01:51:29.002313221, Loss: 0.3569, Nodes_count: 2011, Cost Time: 4.70s\n",
      "Time: 2018-04-05 01:51:29.002313221~2018-04-05 02:07:14.002084758, Loss: 0.3081, Nodes_count: 2288, Cost Time: 5.38s\n",
      "Time: 2018-04-05 02:07:14.002084758~2018-04-05 02:22:58.868615964, Loss: 0.3045, Nodes_count: 2556, Cost Time: 6.05s\n",
      "Time: 2018-04-05 02:22:58.868615964~2018-04-05 02:38:57.566547699, Loss: 0.3009, Nodes_count: 2834, Cost Time: 6.73s\n",
      "Time: 2018-04-05 02:38:57.566547699~2018-04-05 02:55:00.682607244, Loss: 0.2596, Nodes_count: 3083, Cost Time: 7.41s\n",
      "Time: 2018-04-05 02:55:00.682607244~2018-04-05 03:10:53.001556807, Loss: 0.2593, Nodes_count: 3355, Cost Time: 8.09s\n",
      "Time: 2018-04-05 03:10:53.001556807~2018-04-05 03:26:55.002409884, Loss: 0.2415, Nodes_count: 3595, Cost Time: 8.76s\n",
      "Time: 2018-04-05 03:26:55.002409884~2018-04-05 03:42:55.922811797, Loss: 0.2459, Nodes_count: 3849, Cost Time: 9.44s\n",
      "Time: 2018-04-05 03:42:55.922811797~2018-04-05 03:58:58.002487508, Loss: 0.2539, Nodes_count: 4112, Cost Time: 10.11s\n",
      "Time: 2018-04-05 03:58:58.002487508~2018-04-05 04:14:39.002167197, Loss: 0.2449, Nodes_count: 4357, Cost Time: 10.79s\n",
      "Time: 2018-04-05 04:14:39.002167197~2018-04-05 04:30:11.036760148, Loss: 0.2780, Nodes_count: 4624, Cost Time: 11.47s\n",
      "Time: 2018-04-05 04:30:11.036760148~2018-04-05 04:46:15.001999669, Loss: 0.2520, Nodes_count: 4867, Cost Time: 12.14s\n",
      "Time: 2018-04-05 04:46:15.001999669~2018-04-05 05:02:08.330961687, Loss: 0.2455, Nodes_count: 5128, Cost Time: 12.81s\n",
      "Time: 2018-04-05 05:02:08.330961687~2018-04-05 05:18:09.305140803, Loss: 0.2286, Nodes_count: 5359, Cost Time: 13.49s\n",
      "Time: 2018-04-05 05:18:09.305140803~2018-04-05 05:34:10.385228434, Loss: 0.2406, Nodes_count: 5595, Cost Time: 14.16s\n",
      "Time: 2018-04-05 05:34:10.385228434~2018-04-05 05:50:17.002116243, Loss: 0.2334, Nodes_count: 5831, Cost Time: 14.83s\n",
      "Time: 2018-04-05 05:50:17.002116243~2018-04-05 06:06:12.194198806, Loss: 0.2457, Nodes_count: 6090, Cost Time: 15.51s\n",
      "Time: 2018-04-05 06:06:12.194198806~2018-04-05 06:21:51.002166161, Loss: 0.2633, Nodes_count: 6332, Cost Time: 16.18s\n",
      "Time: 2018-04-05 06:21:51.002166161~2018-04-05 06:37:14.185732860, Loss: 0.2470, Nodes_count: 6576, Cost Time: 16.85s\n",
      "Time: 2018-04-05 06:37:14.185732860~2018-04-05 06:53:08.002087088, Loss: 0.2548, Nodes_count: 6839, Cost Time: 17.53s\n",
      "Time: 2018-04-05 06:53:08.002087088~2018-04-05 07:08:55.933580455, Loss: 0.2659, Nodes_count: 7093, Cost Time: 18.20s\n",
      "Time: 2018-04-05 07:08:55.933580455~2018-04-05 07:24:26.002041412, Loss: 0.2433, Nodes_count: 7332, Cost Time: 18.87s\n",
      "Time: 2018-04-05 07:24:26.002041412~2018-04-05 07:40:17.980414667, Loss: 0.2376, Nodes_count: 7571, Cost Time: 19.55s\n",
      "Time: 2018-04-05 07:40:17.980414667~2018-04-05 07:56:18.956234070, Loss: 0.2412, Nodes_count: 7813, Cost Time: 20.22s\n",
      "Time: 2018-04-05 07:56:18.956234070~2018-04-05 08:12:10.958774075, Loss: 0.2496, Nodes_count: 8045, Cost Time: 20.89s\n",
      "Time: 2018-04-05 08:12:10.958774075~2018-04-05 08:27:50.388175076, Loss: 0.2608, Nodes_count: 8281, Cost Time: 21.57s\n",
      "Time: 2018-04-05 08:27:50.388175076~2018-04-05 08:43:34.002441919, Loss: 0.2438, Nodes_count: 8503, Cost Time: 22.24s\n",
      "Time: 2018-04-05 08:43:34.002441919~2018-04-05 08:59:21.324016815, Loss: 0.2487, Nodes_count: 8733, Cost Time: 22.92s\n",
      "Time: 2018-04-05 08:59:21.324016815~2018-04-05 09:14:25.002153785, Loss: 0.2105, Nodes_count: 8973, Cost Time: 24.55s\n",
      "Time: 2018-04-05 09:14:25.002153785~2018-04-05 09:29:34.002026027, Loss: 0.1370, Nodes_count: 9182, Cost Time: 26.29s\n",
      "Time: 2018-04-05 09:29:34.002026027~2018-04-05 09:45:08.002409602, Loss: 0.0948, Nodes_count: 9407, Cost Time: 29.32s\n",
      "Time: 2018-04-05 09:45:08.002409602~2018-04-05 10:00:29.002311387, Loss: 0.1257, Nodes_count: 9631, Cost Time: 31.28s\n",
      "Time: 2018-04-05 10:00:29.002311387~2018-04-05 10:15:32.017273004, Loss: 0.1263, Nodes_count: 9864, Cost Time: 32.91s\n",
      "Time: 2018-04-05 10:15:32.017273004~2018-04-05 10:31:02.959650978, Loss: 0.1214, Nodes_count: 10530, Cost Time: 38.77s\n",
      "Time: 2018-04-05 10:31:02.959650978~2018-04-05 10:46:05.542479370, Loss: 0.5654, Nodes_count: 15830, Cost Time: 53.83s\n",
      "Time: 2018-04-05 10:46:05.542479370~2018-04-05 11:02:24.772812212, Loss: 0.4802, Nodes_count: 19217, Cost Time: 63.41s\n",
      "Time: 2018-04-05 11:02:24.772812212~2018-04-05 11:17:55.031774102, Loss: 0.2291, Nodes_count: 19433, Cost Time: 64.43s\n",
      "Time: 2018-04-05 11:17:55.031774102~2018-04-05 11:42:09.829507526, Loss: 0.3149, Nodes_count: 19502, Cost Time: 64.52s\n",
      "Time: 2018-04-05 11:42:09.829507526~2018-04-05 11:57:55.227502668, Loss: 0.2931, Nodes_count: 22110, Cost Time: 68.99s\n",
      "Time: 2018-04-05 11:57:55.227502668~2018-04-05 12:12:55.317042545, Loss: 0.2513, Nodes_count: 25948, Cost Time: 77.30s\n",
      "Time: 2018-04-05 12:12:55.317042545~2018-04-05 12:28:10.002061679, Loss: 0.2839, Nodes_count: 28346, Cost Time: 82.10s\n"
     ]
    }
   ],
   "source": [
    "model=torch.load(\"./models/model_saved_emb100_BATCH_1024_LastAggregator_multiclass_without_neg_edge.pt\",map_location=device)\n",
    "memory,gnn, link_pred,neighbor_loader=model\n",
    "ans_4_5=test_day_new(graph_4_5,\"graph_4_5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after merge: TemporalData(dst=[685635], msg=[685635, 41], src=[685635], t=[685635])\n",
      "Time: 2018-04-09 08:46:55.004764124~2018-04-09 09:03:31.001287346, Loss: 2.7055, Nodes_count: 27, Cost Time: 0.06s\n",
      "Time: 2018-04-09 09:03:31.001287346~2018-04-09 09:20:23.001295997, Loss: 0.0954, Nodes_count: 67, Cost Time: 0.40s\n",
      "Time: 2018-04-09 09:20:23.001295997~2018-04-09 09:36:02.001305059, Loss: 0.1308, Nodes_count: 100, Cost Time: 0.65s\n",
      "Time: 2018-04-09 09:36:02.001305059~2018-04-09 09:51:31.358485271, Loss: 0.5903, Nodes_count: 5561, Cost Time: 2.32s\n",
      "Time: 2018-04-09 09:51:31.358485271~2018-04-09 10:06:48.907525397, Loss: 0.4205, Nodes_count: 6439, Cost Time: 5.03s\n",
      "Time: 2018-04-09 10:06:48.907525397~2018-04-09 10:23:05.001376520, Loss: 0.4420, Nodes_count: 13241, Cost Time: 9.70s\n",
      "Time: 2018-04-09 10:23:05.001376520~2018-04-09 10:39:33.001366978, Loss: 0.3917, Nodes_count: 13594, Cost Time: 11.02s\n",
      "Time: 2018-04-09 10:39:33.001366978~2018-04-09 10:58:03.657948688, Loss: 0.2286, Nodes_count: 13849, Cost Time: 11.68s\n",
      "Time: 2018-04-09 10:58:03.657948688~2018-04-09 11:13:16.001349955, Loss: 0.3975, Nodes_count: 14830, Cost Time: 14.02s\n",
      "Time: 2018-04-09 11:13:16.001349955~2018-04-09 11:29:58.243560095, Loss: 0.4314, Nodes_count: 14967, Cost Time: 15.34s\n",
      "Time: 2018-04-09 11:29:58.243560095~2018-04-09 11:48:04.809192587, Loss: 0.4447, Nodes_count: 15191, Cost Time: 16.16s\n",
      "Time: 2018-04-09 11:48:04.809192587~2018-04-09 12:03:32.001348097, Loss: 0.2204, Nodes_count: 15262, Cost Time: 16.53s\n",
      "Time: 2018-04-09 12:03:32.001348097~2018-04-09 12:20:47.001308311, Loss: 0.0757, Nodes_count: 15326, Cost Time: 17.12s\n",
      "Time: 2018-04-09 12:20:47.001308311~2018-04-09 12:39:09.419323601, Loss: 0.1252, Nodes_count: 15361, Cost Time: 17.37s\n",
      "Time: 2018-04-09 12:39:09.419323601~2018-04-09 12:57:31.001362714, Loss: 0.1560, Nodes_count: 15395, Cost Time: 17.61s\n",
      "Time: 2018-04-09 12:57:31.001362714~2018-04-09 13:15:43.001302338, Loss: 0.0865, Nodes_count: 15442, Cost Time: 18.24s\n",
      "Time: 2018-04-09 13:15:43.001302338~2018-04-09 13:33:39.419905659, Loss: 0.5797, Nodes_count: 23044, Cost Time: 21.96s\n",
      "Time: 2018-04-09 13:33:39.419905659~2018-04-09 13:51:11.470466610, Loss: 0.3296, Nodes_count: 23501, Cost Time: 23.65s\n",
      "Time: 2018-04-09 13:51:11.470466610~2018-04-09 14:07:06.153449737, Loss: 0.1117, Nodes_count: 23530, Cost Time: 23.94s\n",
      "Time: 2018-04-09 14:07:06.153449737~2018-04-09 14:22:13.001279667, Loss: 0.4903, Nodes_count: 24119, Cost Time: 25.51s\n",
      "Time: 2018-04-09 14:22:13.001279667~2018-04-09 14:37:23.941320412, Loss: 0.5531, Nodes_count: 24694, Cost Time: 27.47s\n",
      "Time: 2018-04-09 14:37:23.941320412~2018-04-09 14:55:15.001271684, Loss: 0.0598, Nodes_count: 24732, Cost Time: 28.06s\n",
      "Time: 2018-04-09 14:55:15.001271684~2018-04-09 15:10:49.001341356, Loss: 0.0637, Nodes_count: 24769, Cost Time: 28.58s\n",
      "Time: 2018-04-09 15:10:49.001341356~2018-04-09 15:26:57.001352104, Loss: 0.1428, Nodes_count: 24804, Cost Time: 28.78s\n",
      "Time: 2018-04-09 15:26:57.001352104~2018-04-09 15:45:04.001267664, Loss: 0.1628, Nodes_count: 24855, Cost Time: 29.01s\n",
      "Time: 2018-04-09 15:45:04.001267664~2018-04-09 16:03:49.001367284, Loss: 0.1310, Nodes_count: 24884, Cost Time: 29.25s\n",
      "Time: 2018-04-09 16:03:49.001367284~2018-04-09 16:21:55.001365897, Loss: 0.1408, Nodes_count: 24941, Cost Time: 29.54s\n",
      "Time: 2018-04-09 16:21:55.001365897~2018-04-09 16:37:01.656413926, Loss: 0.0629, Nodes_count: 24989, Cost Time: 30.29s\n",
      "Time: 2018-04-09 16:37:01.656413926~2018-04-09 16:52:03.637955316, Loss: 0.3519, Nodes_count: 25780, Cost Time: 32.08s\n",
      "Time: 2018-04-09 16:52:03.637955316~2018-04-09 17:21:00.121556708, Loss: 0.4475, Nodes_count: 27147, Cost Time: 34.60s\n",
      "Time: 2018-04-09 17:21:00.121556708~2018-04-09 17:36:15.628929147, Loss: 0.4787, Nodes_count: 28402, Cost Time: 37.58s\n",
      "Time: 2018-04-09 17:36:15.628929147~2018-04-09 17:53:19.648909478, Loss: 0.3990, Nodes_count: 29065, Cost Time: 39.88s\n",
      "Time: 2018-04-09 17:53:19.648909478~2018-04-09 18:12:18.001372515, Loss: 0.4422, Nodes_count: 29530, Cost Time: 41.80s\n"
     ]
    }
   ],
   "source": [
    "model=torch.load(\"./models/model_saved_emb100_BATCH_1024_LastAggregator_multiclass_without_neg_edge.pt\",map_location=device)\n",
    "memory,gnn, link_pred,neighbor_loader=model\n",
    "ans_4_9=test_day_new(graph_4_9,\"graph_4_9\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after merge: TemporalData(dst=[6274151], msg=[6274151, 41], src=[6274151], t=[6274151])\n",
      "Time: 2018-04-10 12:44:33.449564893~2018-04-10 13:00:02.700560774, Loss: 0.5856, Nodes_count: 701, Cost Time: 0.31s\n",
      "Time: 2018-04-10 13:00:02.700560774~2018-04-10 13:16:13.551944728, Loss: 0.1325, Nodes_count: 1373, Cost Time: 6.94s\n",
      "Time: 2018-04-10 13:16:13.551944728~2018-04-10 13:31:14.548738409, Loss: 0.3608, Nodes_count: 6803, Cost Time: 14.11s\n",
      "Time: 2018-04-10 13:31:14.548738409~2018-04-10 13:46:36.161065223, Loss: 0.3789, Nodes_count: 11267, Cost Time: 27.42s\n",
      "Time: 2018-04-10 13:46:36.161065223~2018-04-10 14:02:17.001271389, Loss: 0.2601, Nodes_count: 11336, Cost Time: 28.56s\n",
      "Time: 2018-04-10 14:02:17.001271389~2018-04-10 14:17:34.001373488, Loss: 0.3190, Nodes_count: 11950, Cost Time: 30.28s\n",
      "Time: 2018-04-10 14:17:34.001373488~2018-04-10 14:33:18.350772859, Loss: 0.1114, Nodes_count: 13208, Cost Time: 168.59s\n",
      "Time: 2018-04-10 14:33:18.350772859~2018-04-10 14:48:47.320442910, Loss: 0.3557, Nodes_count: 14409, Cost Time: 179.83s\n",
      "Time: 2018-04-10 14:48:47.320442910~2018-04-10 15:03:54.307022037, Loss: 0.3346, Nodes_count: 23282, Cost Time: 188.10s\n",
      "Time: 2018-04-10 15:03:54.307022037~2018-04-10 15:19:25.001773315, Loss: 0.3651, Nodes_count: 28511, Cost Time: 198.37s\n",
      "Time: 2018-04-10 15:19:25.001773315~2018-04-10 15:36:13.002273705, Loss: 0.2881, Nodes_count: 29263, Cost Time: 200.31s\n",
      "Time: 2018-04-10 15:36:13.002273705~2018-04-10 15:51:24.614585595, Loss: 0.2163, Nodes_count: 29817, Cost Time: 201.67s\n",
      "Time: 2018-04-10 15:51:24.614585595~2018-04-10 16:06:34.924760399, Loss: 0.2001, Nodes_count: 30371, Cost Time: 204.82s\n",
      "Time: 2018-04-10 16:06:34.924760399~2018-04-10 16:21:58.949085533, Loss: 0.2594, Nodes_count: 35259, Cost Time: 213.78s\n",
      "Time: 2018-04-10 16:21:58.949085533~2018-04-10 16:37:32.001491870, Loss: 0.2014, Nodes_count: 39140, Cost Time: 225.04s\n",
      "Time: 2018-04-10 16:37:32.001491870~2018-04-10 16:52:38.298464794, Loss: 0.3654, Nodes_count: 43466, Cost Time: 234.97s\n",
      "Time: 2018-04-10 16:52:38.298464794~2018-04-10 17:07:45.640852929, Loss: 0.3214, Nodes_count: 49082, Cost Time: 249.62s\n",
      "Time: 2018-04-10 17:07:45.640852929~2018-04-10 17:22:45.926569237, Loss: 0.1866, Nodes_count: 51813, Cost Time: 260.32s\n",
      "Time: 2018-04-10 17:22:45.926569237~2018-04-10 17:37:49.534629845, Loss: 0.2227, Nodes_count: 52495, Cost Time: 266.92s\n",
      "Time: 2018-04-10 17:37:49.534629845~2018-04-10 17:52:50.724731198, Loss: 0.2342, Nodes_count: 57352, Cost Time: 277.91s\n",
      "Time: 2018-04-10 17:52:50.724731198~2018-04-10 18:08:02.001452859, Loss: 0.3308, Nodes_count: 62190, Cost Time: 288.51s\n",
      "Time: 2018-04-10 18:08:02.001452859~2018-04-10 18:23:10.812709243, Loss: 0.3745, Nodes_count: 68455, Cost Time: 300.64s\n",
      "Time: 2018-04-10 18:23:10.812709243~2018-04-10 18:38:10.882714584, Loss: 0.3490, Nodes_count: 76397, Cost Time: 314.96s\n",
      "Time: 2018-04-10 18:38:10.882714584~2018-04-10 18:54:51.062762969, Loss: 0.1894, Nodes_count: 79099, Cost Time: 323.98s\n",
      "Time: 2018-04-10 18:54:51.062762969~2018-04-10 19:10:03.757528839, Loss: 0.4466, Nodes_count: 82842, Cost Time: 331.37s\n",
      "Time: 2018-04-10 19:10:03.757528839~2018-04-10 19:26:29.001922190, Loss: 0.3101, Nodes_count: 83993, Cost Time: 333.37s\n",
      "Time: 2018-04-10 19:26:29.001922190~2018-04-10 19:41:52.912938380, Loss: 0.1908, Nodes_count: 84614, Cost Time: 334.88s\n",
      "Time: 2018-04-10 19:41:52.912938380~2018-04-10 19:57:35.001557519, Loss: 0.2864, Nodes_count: 86372, Cost Time: 338.35s\n",
      "Time: 2018-04-10 19:57:35.001557519~2018-04-10 20:13:24.002153177, Loss: 0.2273, Nodes_count: 87193, Cost Time: 340.12s\n",
      "Time: 2018-04-10 20:13:24.002153177~2018-04-10 20:29:32.050293487, Loss: 0.2493, Nodes_count: 88890, Cost Time: 343.66s\n",
      "Time: 2018-04-10 20:29:32.050293487~2018-04-10 20:44:46.874595566, Loss: 0.2445, Nodes_count: 90492, Cost Time: 347.18s\n",
      "Time: 2018-04-10 20:44:46.874595566~2018-04-10 21:00:56.078860802, Loss: 0.1282, Nodes_count: 90591, Cost Time: 347.86s\n",
      "Time: 2018-04-10 21:00:56.078860802~2018-04-10 21:16:35.002073797, Loss: 0.2286, Nodes_count: 92025, Cost Time: 351.12s\n",
      "Time: 2018-04-10 21:16:35.002073797~2018-04-10 21:32:59.002389345, Loss: 0.1831, Nodes_count: 92511, Cost Time: 352.73s\n",
      "Time: 2018-04-10 21:32:59.002389345~2018-04-10 21:48:01.438127726, Loss: 0.1807, Nodes_count: 92798, Cost Time: 353.97s\n",
      "Time: 2018-04-10 21:48:01.438127726~2018-04-10 22:03:05.222735201, Loss: 0.2285, Nodes_count: 95322, Cost Time: 358.37s\n",
      "Time: 2018-04-10 22:03:05.222735201~2018-04-10 22:18:56.001694554, Loss: 0.2313, Nodes_count: 97227, Cost Time: 363.00s\n",
      "Time: 2018-04-10 22:18:56.001694554~2018-04-10 22:35:47.001550655, Loss: 0.2262, Nodes_count: 99283, Cost Time: 367.88s\n",
      "Time: 2018-04-10 22:35:47.001550655~2018-04-10 22:51:30.001246535, Loss: 0.2444, Nodes_count: 100995, Cost Time: 371.60s\n",
      "Time: 2018-04-10 22:51:30.001246535~2018-04-10 23:07:01.106168658, Loss: 0.1084, Nodes_count: 101055, Cost Time: 372.24s\n",
      "Time: 2018-04-10 23:07:01.106168658~2018-04-10 23:22:23.515914390, Loss: 0.1222, Nodes_count: 101109, Cost Time: 372.72s\n",
      "Time: 2018-04-10 23:22:23.515914390~2018-04-10 23:38:02.289791440, Loss: 0.1198, Nodes_count: 101168, Cost Time: 373.20s\n",
      "Time: 2018-04-10 23:38:02.289791440~2018-04-10 23:53:53.001127888, Loss: 0.1069, Nodes_count: 101210, Cost Time: 373.68s\n"
     ]
    }
   ],
   "source": [
    "model=torch.load(\"./models/model_saved_emb100_BATCH_1024_LastAggregator_multiclass_without_neg_edge.pt\",map_location=device)\n",
    "memory,gnn, link_pred,neighbor_loader=model\n",
    "ans_4_10=test_day_new(graph_4_10,\"graph_4_10\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after merge: TemporalData(dst=[7285220], msg=[7285220, 41], src=[7285220], t=[7285220])\n",
      "Time: 2018-04-11 00:00:00.001151329~2018-04-11 00:16:06.001274623, Loss: 0.4609, Nodes_count: 117, Cost Time: 0.57s\n",
      "Time: 2018-04-11 00:16:06.001274623~2018-04-11 00:32:08.001169192, Loss: 0.2324, Nodes_count: 257, Cost Time: 1.18s\n",
      "Time: 2018-04-11 00:32:08.001169192~2018-04-11 00:48:31.001594545, Loss: 0.3004, Nodes_count: 336, Cost Time: 1.78s\n",
      "Time: 2018-04-11 00:48:31.001594545~2018-04-11 01:04:53.001888013, Loss: 0.3110, Nodes_count: 413, Cost Time: 2.38s\n",
      "Time: 2018-04-11 01:04:53.001888013~2018-04-11 01:20:59.002307553, Loss: 0.5455, Nodes_count: 492, Cost Time: 2.99s\n",
      "Time: 2018-04-11 01:20:59.002307553~2018-04-11 01:37:06.826246565, Loss: 0.1990, Nodes_count: 575, Cost Time: 3.59s\n",
      "Time: 2018-04-11 01:37:06.826246565~2018-04-11 01:53:33.001963923, Loss: 0.2920, Nodes_count: 649, Cost Time: 4.19s\n",
      "Time: 2018-04-11 01:53:33.001963923~2018-04-11 02:10:00.001194307, Loss: 0.4178, Nodes_count: 724, Cost Time: 4.79s\n",
      "Time: 2018-04-11 02:10:00.001194307~2018-04-11 02:26:03.001337306, Loss: 0.8949, Nodes_count: 803, Cost Time: 5.40s\n",
      "Time: 2018-04-11 02:26:03.001337306~2018-04-11 02:42:12.001881387, Loss: 1.2012, Nodes_count: 886, Cost Time: 6.00s\n",
      "Time: 2018-04-11 02:42:12.001881387~2018-04-11 02:58:40.001723965, Loss: 1.2081, Nodes_count: 958, Cost Time: 6.60s\n",
      "Time: 2018-04-11 02:58:40.001723965~2018-04-11 03:15:01.001706699, Loss: 1.6633, Nodes_count: 1034, Cost Time: 7.20s\n",
      "Time: 2018-04-11 03:15:01.001706699~2018-04-11 03:31:06.001379178, Loss: 1.8652, Nodes_count: 1116, Cost Time: 7.80s\n",
      "Time: 2018-04-11 03:31:06.001379178~2018-04-11 03:47:16.001779251, Loss: 1.7576, Nodes_count: 1194, Cost Time: 8.40s\n",
      "Time: 2018-04-11 03:47:16.001779251~2018-04-11 04:03:26.001627450, Loss: 1.2365, Nodes_count: 1305, Cost Time: 9.00s\n",
      "Time: 2018-04-11 04:03:26.001627450~2018-04-11 04:19:33.002034536, Loss: 0.7077, Nodes_count: 1385, Cost Time: 9.61s\n",
      "Time: 2018-04-11 04:19:33.002034536~2018-04-11 04:35:40.001832776, Loss: 0.6997, Nodes_count: 1474, Cost Time: 10.21s\n",
      "Time: 2018-04-11 04:35:40.001832776~2018-04-11 04:52:02.636356915, Loss: 0.2851, Nodes_count: 1548, Cost Time: 10.81s\n",
      "Time: 2018-04-11 04:52:02.636356915~2018-04-11 05:08:14.807062185, Loss: 0.1945, Nodes_count: 1625, Cost Time: 11.41s\n",
      "Time: 2018-04-11 05:08:14.807062185~2018-04-11 05:24:22.001019938, Loss: 0.2200, Nodes_count: 1703, Cost Time: 12.01s\n",
      "Time: 2018-04-11 05:24:22.001019938~2018-04-11 05:40:42.002091762, Loss: 0.1659, Nodes_count: 1782, Cost Time: 12.62s\n",
      "Time: 2018-04-11 05:40:42.002091762~2018-04-11 05:57:05.001981906, Loss: 0.3887, Nodes_count: 1858, Cost Time: 13.22s\n",
      "Time: 2018-04-11 05:57:05.001981906~2018-04-11 06:13:20.001263471, Loss: 0.5418, Nodes_count: 1937, Cost Time: 13.82s\n",
      "Time: 2018-04-11 06:13:20.001263471~2018-04-11 06:29:17.894355498, Loss: 0.4678, Nodes_count: 2017, Cost Time: 14.42s\n",
      "Time: 2018-04-11 06:29:17.894355498~2018-04-11 06:45:36.001432729, Loss: 0.7187, Nodes_count: 2098, Cost Time: 15.02s\n",
      "Time: 2018-04-11 06:45:36.001432729~2018-04-11 07:02:01.002206348, Loss: 0.7544, Nodes_count: 2172, Cost Time: 15.62s\n",
      "Time: 2018-04-11 07:02:01.002206348~2018-04-11 07:18:14.001812105, Loss: 0.3866, Nodes_count: 4105, Cost Time: 17.95s\n",
      "Time: 2018-04-11 07:18:14.001812105~2018-04-11 07:34:30.001240773, Loss: 0.2700, Nodes_count: 7127, Cost Time: 23.17s\n",
      "Time: 2018-04-11 07:34:30.001240773~2018-04-11 07:49:43.130898948, Loss: 1.0635, Nodes_count: 18603, Cost Time: 38.82s\n",
      "Time: 2018-04-11 07:49:43.130898948~2018-04-11 08:05:01.864920978, Loss: 0.2041, Nodes_count: 19503, Cost Time: 41.75s\n",
      "Time: 2018-04-11 08:05:01.864920978~2018-04-11 08:20:03.240565286, Loss: 0.2300, Nodes_count: 22051, Cost Time: 46.71s\n",
      "Time: 2018-04-11 08:20:03.240565286~2018-04-11 08:35:12.182436041, Loss: 0.2550, Nodes_count: 26081, Cost Time: 53.79s\n",
      "Time: 2018-04-11 08:35:12.182436041~2018-04-11 08:51:45.001713561, Loss: 0.2212, Nodes_count: 27162, Cost Time: 57.06s\n",
      "Time: 2018-04-11 08:51:45.001713561~2018-04-11 09:07:41.001516635, Loss: 0.5578, Nodes_count: 39617, Cost Time: 65.03s\n",
      "Time: 2018-04-11 09:07:41.001516635~2018-04-11 09:23:47.001103293, Loss: 0.2322, Nodes_count: 40216, Cost Time: 69.48s\n",
      "Time: 2018-04-11 09:23:47.001103293~2018-04-11 09:38:59.728361285, Loss: 0.3044, Nodes_count: 44684, Cost Time: 76.90s\n",
      "Time: 2018-04-11 09:38:59.728361285~2018-04-11 09:54:01.833145793, Loss: 0.2442, Nodes_count: 49845, Cost Time: 85.81s\n",
      "Time: 2018-04-11 09:54:01.833145793~2018-04-11 10:09:03.508820548, Loss: 0.3050, Nodes_count: 54800, Cost Time: 95.92s\n",
      "Time: 2018-04-11 10:09:03.508820548~2018-04-11 10:24:19.673656595, Loss: 0.2755, Nodes_count: 58795, Cost Time: 107.52s\n",
      "Time: 2018-04-11 10:24:19.673656595~2018-04-11 10:39:40.241867510, Loss: 0.3221, Nodes_count: 64215, Cost Time: 118.42s\n",
      "Time: 2018-04-11 10:39:40.241867510~2018-04-11 10:56:00.001497677, Loss: 0.4253, Nodes_count: 70318, Cost Time: 131.93s\n",
      "Time: 2018-04-11 10:56:00.001497677~2018-04-11 11:11:11.539494920, Loss: 0.2199, Nodes_count: 73238, Cost Time: 139.62s\n",
      "Time: 2018-04-11 11:11:11.539494920~2018-04-11 11:26:42.586327848, Loss: 0.2931, Nodes_count: 76505, Cost Time: 149.07s\n",
      "Time: 2018-04-11 11:26:42.586327848~2018-04-11 11:41:54.164805680, Loss: 0.4173, Nodes_count: 85321, Cost Time: 166.89s\n",
      "Time: 2018-04-11 11:41:54.164805680~2018-04-11 11:57:06.982513263, Loss: 0.1185, Nodes_count: 86919, Cost Time: 177.09s\n",
      "Time: 2018-04-11 11:57:06.982513263~2018-04-11 12:13:41.616470541, Loss: 0.2557, Nodes_count: 89260, Cost Time: 182.30s\n",
      "Time: 2018-04-11 12:13:41.616470541~2018-04-11 12:29:18.001334304, Loss: 0.2221, Nodes_count: 90405, Cost Time: 185.57s\n",
      "Time: 2018-04-11 12:29:18.001334304~2018-04-11 12:44:47.002346666, Loss: 0.2536, Nodes_count: 91765, Cost Time: 188.27s\n",
      "Time: 2018-04-11 12:44:47.002346666~2018-04-11 13:01:13.002135659, Loss: 0.1977, Nodes_count: 92569, Cost Time: 190.10s\n",
      "Time: 2018-04-11 13:01:13.002135659~2018-04-11 13:16:50.021059784, Loss: 0.1696, Nodes_count: 95156, Cost Time: 199.30s\n",
      "Time: 2018-04-11 13:16:50.021059784~2018-04-11 13:31:56.533212332, Loss: 0.2296, Nodes_count: 99200, Cost Time: 208.40s\n",
      "Time: 2018-04-11 13:31:56.533212332~2018-04-11 13:46:57.298929583, Loss: 0.3895, Nodes_count: 105598, Cost Time: 224.42s\n",
      "Time: 2018-04-11 13:46:57.298929583~2018-04-11 14:02:03.185872207, Loss: 0.3952, Nodes_count: 111689, Cost Time: 237.80s\n",
      "Time: 2018-04-11 14:02:03.185872207~2018-04-11 14:17:10.448756073, Loss: 0.3638, Nodes_count: 116717, Cost Time: 248.81s\n",
      "Time: 2018-04-11 14:17:10.448756073~2018-04-11 14:32:13.526157650, Loss: 0.2660, Nodes_count: 121392, Cost Time: 258.87s\n",
      "Time: 2018-04-11 14:32:13.526157650~2018-04-11 14:47:23.351053223, Loss: 0.3157, Nodes_count: 124668, Cost Time: 273.85s\n",
      "Time: 2018-04-11 14:47:23.351053223~2018-04-11 15:03:33.590152518, Loss: 0.4103, Nodes_count: 125975, Cost Time: 281.37s\n",
      "Time: 2018-04-11 15:03:33.590152518~2018-04-11 15:20:05.001754985, Loss: 0.2149, Nodes_count: 127668, Cost Time: 286.54s\n",
      "Time: 2018-04-11 15:20:05.001754985~2018-04-11 15:36:18.002102989, Loss: 0.2383, Nodes_count: 128810, Cost Time: 290.07s\n",
      "Time: 2018-04-11 15:36:18.002102989~2018-04-11 15:52:42.862552619, Loss: 0.2396, Nodes_count: 130028, Cost Time: 293.37s\n",
      "Time: 2018-04-11 15:52:42.862552619~2018-04-11 16:08:52.001653616, Loss: 0.3296, Nodes_count: 130617, Cost Time: 298.16s\n",
      "Time: 2018-04-11 16:08:52.001653616~2018-04-11 16:23:53.622260753, Loss: 0.2443, Nodes_count: 133963, Cost Time: 305.05s\n",
      "Time: 2018-04-11 16:23:53.622260753~2018-04-11 16:40:20.001018770, Loss: 0.2106, Nodes_count: 135480, Cost Time: 315.62s\n",
      "Time: 2018-04-11 16:40:20.001018770~2018-04-11 16:55:20.537127474, Loss: 0.3167, Nodes_count: 135918, Cost Time: 319.88s\n",
      "Time: 2018-04-11 16:55:20.537127474~2018-04-11 17:11:02.002108596, Loss: 0.2340, Nodes_count: 136364, Cost Time: 324.81s\n",
      "Time: 2018-04-11 17:11:02.002108596~2018-04-11 17:26:02.530643144, Loss: 0.2807, Nodes_count: 137349, Cost Time: 329.23s\n",
      "Time: 2018-04-11 17:26:02.530643144~2018-04-11 17:41:17.001646675, Loss: 0.3325, Nodes_count: 140172, Cost Time: 338.38s\n",
      "Time: 2018-04-11 17:41:17.001646675~2018-04-11 17:56:19.699660881, Loss: 0.2389, Nodes_count: 143918, Cost Time: 347.67s\n",
      "Time: 2018-04-11 17:56:19.699660881~2018-04-11 18:11:46.356548226, Loss: 0.2492, Nodes_count: 147999, Cost Time: 359.33s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time: 2018-04-11 18:11:46.356548226~2018-04-11 18:26:53.001667593, Loss: 0.2475, Nodes_count: 152325, Cost Time: 369.77s\n",
      "Time: 2018-04-11 18:26:53.001667593~2018-04-11 18:42:17.001965855, Loss: 0.3416, Nodes_count: 158107, Cost Time: 384.43s\n",
      "Time: 2018-04-11 18:42:17.001965855~2018-04-11 18:58:43.001951167, Loss: 0.1245, Nodes_count: 158194, Cost Time: 387.33s\n",
      "Time: 2018-04-11 18:58:43.001951167~2018-04-11 19:14:49.001363010, Loss: 0.1575, Nodes_count: 159108, Cost Time: 392.48s\n",
      "Time: 2018-04-11 19:14:49.001363010~2018-04-11 19:30:09.001780046, Loss: 0.2217, Nodes_count: 161006, Cost Time: 397.82s\n",
      "Time: 2018-04-11 19:30:09.001780046~2018-04-11 19:46:28.001487257, Loss: 0.0950, Nodes_count: 161049, Cost Time: 398.65s\n",
      "Time: 2018-04-11 19:46:28.001487257~2018-04-11 20:02:07.594306306, Loss: 0.2244, Nodes_count: 162909, Cost Time: 403.59s\n",
      "Time: 2018-04-11 20:02:07.594306306~2018-04-11 20:17:10.001507207, Loss: 0.1936, Nodes_count: 163389, Cost Time: 405.32s\n",
      "Time: 2018-04-11 20:17:10.001507207~2018-04-11 20:33:47.001341878, Loss: 0.2252, Nodes_count: 165426, Cost Time: 410.73s\n",
      "Time: 2018-04-11 20:33:47.001341878~2018-04-11 20:48:52.002055784, Loss: 0.1993, Nodes_count: 166148, Cost Time: 413.25s\n",
      "Time: 2018-04-11 20:48:52.002055784~2018-04-11 21:03:56.946916791, Loss: 0.2055, Nodes_count: 167446, Cost Time: 416.63s\n",
      "Time: 2018-04-11 21:03:56.946916791~2018-04-11 21:20:04.001983239, Loss: 0.2347, Nodes_count: 169242, Cost Time: 421.07s\n",
      "Time: 2018-04-11 21:20:04.001983239~2018-04-11 21:36:36.001464315, Loss: 0.2188, Nodes_count: 170776, Cost Time: 425.25s\n",
      "Time: 2018-04-11 21:36:36.001464315~2018-04-11 21:53:05.001134741, Loss: 0.2373, Nodes_count: 172882, Cost Time: 429.54s\n",
      "Time: 2018-04-11 21:53:05.001134741~2018-04-11 22:08:43.001339416, Loss: 0.2065, Nodes_count: 174210, Cost Time: 433.86s\n",
      "Time: 2018-04-11 22:08:43.001339416~2018-04-11 22:24:17.001832396, Loss: 0.1982, Nodes_count: 174916, Cost Time: 436.37s\n",
      "Time: 2018-04-11 22:24:17.001832396~2018-04-11 22:39:54.002070033, Loss: 0.2315, Nodes_count: 176672, Cost Time: 440.24s\n",
      "Time: 2018-04-11 22:39:54.002070033~2018-04-11 22:56:00.892549695, Loss: 0.1108, Nodes_count: 176717, Cost Time: 441.00s\n",
      "Time: 2018-04-11 22:56:00.892549695~2018-04-11 23:11:03.001690405, Loss: 0.2376, Nodes_count: 178652, Cost Time: 446.10s\n",
      "Time: 2018-04-11 23:11:03.001690405~2018-04-11 23:26:56.002055798, Loss: 0.1138, Nodes_count: 178701, Cost Time: 446.92s\n",
      "Time: 2018-04-11 23:26:56.002055798~2018-04-11 23:43:02.705859964, Loss: 0.0921, Nodes_count: 178752, Cost Time: 447.52s\n",
      "Time: 2018-04-11 23:43:02.705859964~2018-04-11 23:59:03.355632261, Loss: 0.0980, Nodes_count: 178795, Cost Time: 448.12s\n"
     ]
    }
   ],
   "source": [
    "model=torch.load(\"./models/model_saved_emb100_BATCH_1024_LastAggregator_multiclass_without_neg_edge.pt\",map_location=device)\n",
    "memory,gnn, link_pred,neighbor_loader=model\n",
    "ans_4_11=test_day_new(graph_4_11,\"graph_4_11\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after merge: TemporalData(dst=[7024937], msg=[7024937, 41], src=[7024937], t=[7024937])\n",
      "Time: 2018-04-12 00:00:00.001773757~2018-04-12 00:15:26.001647081, Loss: 0.5566, Nodes_count: 120, Cost Time: 0.47s\n",
      "Time: 2018-04-12 00:15:26.001647081~2018-04-12 00:30:58.002002412, Loss: 0.1614, Nodes_count: 252, Cost Time: 0.95s\n",
      "Time: 2018-04-12 00:30:58.002002412~2018-04-12 00:46:27.001835122, Loss: 0.1458, Nodes_count: 330, Cost Time: 1.44s\n",
      "Time: 2018-04-12 00:46:27.001835122~2018-04-12 01:02:05.698953597, Loss: 0.1299, Nodes_count: 402, Cost Time: 1.92s\n",
      "Time: 2018-04-12 01:02:05.698953597~2018-04-12 01:17:27.002277528, Loss: 0.1222, Nodes_count: 477, Cost Time: 2.41s\n",
      "Time: 2018-04-12 01:17:27.002277528~2018-04-12 01:32:48.001732365, Loss: 0.1568, Nodes_count: 594, Cost Time: 2.89s\n",
      "Time: 2018-04-12 01:32:48.001732365~2018-04-12 01:48:20.001593370, Loss: 0.1356, Nodes_count: 671, Cost Time: 3.38s\n",
      "Time: 2018-04-12 01:48:20.001593370~2018-04-12 02:04:08.001895394, Loss: 0.1275, Nodes_count: 742, Cost Time: 3.86s\n",
      "Time: 2018-04-12 02:04:08.001895394~2018-04-12 02:19:33.000943880, Loss: 0.1483, Nodes_count: 817, Cost Time: 4.35s\n",
      "Time: 2018-04-12 02:19:33.000943880~2018-04-12 02:35:09.219021781, Loss: 0.1293, Nodes_count: 895, Cost Time: 4.83s\n",
      "Time: 2018-04-12 02:35:09.219021781~2018-04-12 02:50:55.001941925, Loss: 0.1310, Nodes_count: 970, Cost Time: 5.31s\n",
      "Time: 2018-04-12 02:50:55.001941925~2018-04-12 03:06:27.001769928, Loss: 0.1370, Nodes_count: 1043, Cost Time: 5.80s\n",
      "Time: 2018-04-12 03:06:27.001769928~2018-04-12 03:22:01.001281628, Loss: 0.1506, Nodes_count: 1117, Cost Time: 6.29s\n",
      "Time: 2018-04-12 03:22:01.001281628~2018-04-12 03:37:29.838784853, Loss: 0.1450, Nodes_count: 1195, Cost Time: 6.77s\n",
      "Time: 2018-04-12 03:37:29.838784853~2018-04-12 03:53:07.001316649, Loss: 0.1357, Nodes_count: 1267, Cost Time: 7.25s\n",
      "Time: 2018-04-12 03:53:07.001316649~2018-04-12 04:08:50.001184547, Loss: 0.1254, Nodes_count: 1339, Cost Time: 7.74s\n",
      "Time: 2018-04-12 04:08:50.001184547~2018-04-12 04:24:00.001592464, Loss: 0.1622, Nodes_count: 1421, Cost Time: 8.22s\n",
      "Time: 2018-04-12 04:24:00.001592464~2018-04-12 04:39:26.002162640, Loss: 0.1416, Nodes_count: 1497, Cost Time: 8.71s\n",
      "Time: 2018-04-12 04:39:26.002162640~2018-04-12 04:55:10.001772049, Loss: 0.1148, Nodes_count: 1569, Cost Time: 9.19s\n",
      "Time: 2018-04-12 04:55:10.001772049~2018-04-12 05:10:51.001908824, Loss: 0.1210, Nodes_count: 1640, Cost Time: 9.67s\n",
      "Time: 2018-04-12 05:10:51.001908824~2018-04-12 05:26:15.658033392, Loss: 0.1334, Nodes_count: 1714, Cost Time: 10.16s\n",
      "Time: 2018-04-12 05:26:15.658033392~2018-04-12 05:41:53.000960686, Loss: 0.1315, Nodes_count: 1792, Cost Time: 10.64s\n",
      "Time: 2018-04-12 05:41:53.000960686~2018-04-12 05:57:26.001519319, Loss: 0.1290, Nodes_count: 1864, Cost Time: 11.13s\n",
      "Time: 2018-04-12 05:57:26.001519319~2018-04-12 06:13:11.001365623, Loss: 0.1428, Nodes_count: 1935, Cost Time: 11.61s\n",
      "Time: 2018-04-12 06:13:11.001365623~2018-04-12 06:28:17.995464541, Loss: 0.1327, Nodes_count: 2013, Cost Time: 12.09s\n",
      "Time: 2018-04-12 06:28:17.995464541~2018-04-12 06:44:00.001985599, Loss: 0.1383, Nodes_count: 2091, Cost Time: 12.57s\n",
      "Time: 2018-04-12 06:44:00.001985599~2018-04-12 06:59:19.188577225, Loss: 0.1487, Nodes_count: 2172, Cost Time: 13.06s\n",
      "Time: 2018-04-12 06:59:19.188577225~2018-04-12 07:14:23.001990475, Loss: 0.4047, Nodes_count: 4649, Cost Time: 15.58s\n",
      "Time: 2018-04-12 07:14:23.001990475~2018-04-12 07:29:24.250402681, Loss: 0.2846, Nodes_count: 5864, Cost Time: 17.55s\n",
      "Time: 2018-04-12 07:29:24.250402681~2018-04-12 07:46:06.002011766, Loss: 0.5597, Nodes_count: 14298, Cost Time: 22.42s\n",
      "Time: 2018-04-12 07:46:06.002011766~2018-04-12 08:02:07.001316523, Loss: 0.2949, Nodes_count: 15664, Cost Time: 24.94s\n",
      "Time: 2018-04-12 08:02:07.001316523~2018-04-12 08:17:47.001755201, Loss: 0.6813, Nodes_count: 37908, Cost Time: 32.50s\n",
      "Time: 2018-04-12 08:17:47.001755201~2018-04-12 08:33:06.483036511, Loss: 0.2816, Nodes_count: 41472, Cost Time: 38.11s\n",
      "Time: 2018-04-12 08:33:06.483036511~2018-04-12 08:48:09.600264510, Loss: 0.1932, Nodes_count: 43829, Cost Time: 44.90s\n",
      "Time: 2018-04-12 08:48:09.600264510~2018-04-12 09:03:50.002384931, Loss: 0.4965, Nodes_count: 47988, Cost Time: 53.07s\n",
      "Time: 2018-04-12 09:03:50.002384931~2018-04-12 09:19:03.668714727, Loss: 0.2608, Nodes_count: 49038, Cost Time: 56.56s\n",
      "Time: 2018-04-12 09:19:03.668714727~2018-04-12 09:34:16.330657912, Loss: 0.2910, Nodes_count: 54792, Cost Time: 65.65s\n",
      "Time: 2018-04-12 09:34:16.330657912~2018-04-12 09:49:24.841744252, Loss: 0.2460, Nodes_count: 60510, Cost Time: 76.97s\n",
      "Time: 2018-04-12 09:49:24.841744252~2018-04-12 10:04:31.967749880, Loss: 0.2506, Nodes_count: 65938, Cost Time: 86.59s\n",
      "Time: 2018-04-12 10:04:31.967749880~2018-04-12 10:19:41.864196505, Loss: 0.2330, Nodes_count: 70646, Cost Time: 96.42s\n",
      "Time: 2018-04-12 10:19:41.864196505~2018-04-12 10:34:55.515804775, Loss: 0.2616, Nodes_count: 74856, Cost Time: 105.08s\n",
      "Time: 2018-04-12 10:34:55.515804775~2018-04-12 10:50:58.001487795, Loss: 0.2060, Nodes_count: 76935, Cost Time: 118.80s\n",
      "Time: 2018-04-12 10:50:58.001487795~2018-04-12 11:06:07.082292087, Loss: 0.2264, Nodes_count: 79564, Cost Time: 125.96s\n",
      "Time: 2018-04-12 11:06:07.082292087~2018-04-12 11:22:02.614075429, Loss: 0.1799, Nodes_count: 82644, Cost Time: 136.00s\n",
      "Time: 2018-04-12 11:22:02.614075429~2018-04-12 11:38:02.721265247, Loss: 0.1804, Nodes_count: 84060, Cost Time: 143.10s\n",
      "Time: 2018-04-12 11:38:02.721265247~2018-04-12 11:53:05.002139204, Loss: 0.2529, Nodes_count: 88361, Cost Time: 151.39s\n",
      "Time: 2018-04-12 11:53:05.002139204~2018-04-12 12:08:50.001259045, Loss: 0.3361, Nodes_count: 90366, Cost Time: 158.42s\n",
      "Time: 2018-04-12 12:08:50.001259045~2018-04-12 12:24:03.925533802, Loss: 0.2159, Nodes_count: 91620, Cost Time: 161.68s\n",
      "Time: 2018-04-12 12:24:03.925533802~2018-04-12 12:39:06.592684498, Loss: 0.2278, Nodes_count: 93203, Cost Time: 164.94s\n",
      "Time: 2018-04-12 12:39:06.592684498~2018-04-12 12:54:44.001888457, Loss: 0.3199, Nodes_count: 95332, Cost Time: 169.24s\n",
      "Time: 2018-04-12 12:54:44.001888457~2018-04-12 13:09:55.026832462, Loss: 0.2738, Nodes_count: 97450, Cost Time: 174.54s\n",
      "Time: 2018-04-12 13:09:55.026832462~2018-04-12 13:25:06.588370709, Loss: 2.5851, Nodes_count: 113686, Cost Time: 181.43s\n",
      "Time: 2018-04-12 13:25:06.588370709~2018-04-12 13:40:07.178206094, Loss: 0.4367, Nodes_count: 115772, Cost Time: 190.28s\n",
      "Time: 2018-04-12 13:40:07.178206094~2018-04-12 13:56:02.001338519, Loss: 0.5002, Nodes_count: 119770, Cost Time: 199.10s\n",
      "Time: 2018-04-12 13:56:02.001338519~2018-04-12 14:11:10.272552800, Loss: 0.1024, Nodes_count: 120208, Cost Time: 209.25s\n",
      "Time: 2018-04-12 14:11:10.272552800~2018-04-12 14:26:13.739235539, Loss: 0.2382, Nodes_count: 121759, Cost Time: 217.94s\n",
      "Time: 2018-04-12 14:26:13.739235539~2018-04-12 14:41:15.795737707, Loss: 0.4219, Nodes_count: 125643, Cost Time: 226.89s\n",
      "Time: 2018-04-12 14:41:15.795737707~2018-04-12 14:56:26.556444597, Loss: 0.2940, Nodes_count: 128246, Cost Time: 232.61s\n",
      "Time: 2018-04-12 14:56:26.556444597~2018-04-12 15:11:30.532680843, Loss: 0.2800, Nodes_count: 130829, Cost Time: 238.36s\n",
      "Time: 2018-04-12 15:11:30.532680843~2018-04-12 15:26:31.776115575, Loss: 0.2888, Nodes_count: 133261, Cost Time: 243.63s\n",
      "Time: 2018-04-12 15:26:31.776115575~2018-04-12 15:41:41.913691938, Loss: 0.2847, Nodes_count: 134757, Cost Time: 247.15s\n",
      "Time: 2018-04-12 15:41:41.913691938~2018-04-12 15:56:42.439764347, Loss: 0.2843, Nodes_count: 135733, Cost Time: 249.68s\n",
      "Time: 2018-04-12 15:56:42.439764347~2018-04-12 16:11:42.990798525, Loss: 0.6249, Nodes_count: 136901, Cost Time: 253.52s\n",
      "Time: 2018-04-12 16:11:42.990798525~2018-04-12 16:26:43.075609426, Loss: 0.4760, Nodes_count: 141175, Cost Time: 269.85s\n",
      "Time: 2018-04-12 16:26:43.075609426~2018-04-12 16:42:02.002013322, Loss: 0.5259, Nodes_count: 144849, Cost Time: 280.98s\n",
      "Time: 2018-04-12 16:42:02.002013322~2018-04-12 16:58:02.604248444, Loss: 0.3368, Nodes_count: 146993, Cost Time: 287.48s\n",
      "Time: 2018-04-12 16:58:02.604248444~2018-04-12 17:13:29.514953544, Loss: 0.3076, Nodes_count: 148738, Cost Time: 292.16s\n",
      "Time: 2018-04-12 17:13:29.514953544~2018-04-12 17:28:37.568314356, Loss: 0.2991, Nodes_count: 151178, Cost Time: 298.48s\n",
      "Time: 2018-04-12 17:28:37.568314356~2018-04-12 17:44:07.048741132, Loss: 0.2979, Nodes_count: 152894, Cost Time: 303.27s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time: 2018-04-12 17:44:07.048741132~2018-04-12 17:59:37.072167889, Loss: 0.4532, Nodes_count: 158848, Cost Time: 321.63s\n",
      "Time: 2018-04-12 17:59:37.072167889~2018-04-12 18:14:42.441209885, Loss: 0.4360, Nodes_count: 162472, Cost Time: 333.70s\n",
      "Time: 2018-04-12 18:14:42.441209885~2018-04-12 18:29:50.813709836, Loss: 0.4797, Nodes_count: 168518, Cost Time: 350.34s\n",
      "Time: 2018-04-12 18:29:50.813709836~2018-04-12 18:44:51.996770103, Loss: 0.3091, Nodes_count: 169554, Cost Time: 353.14s\n",
      "Time: 2018-04-12 18:44:51.996770103~2018-04-12 18:59:54.090374716, Loss: 0.3789, Nodes_count: 169776, Cost Time: 355.47s\n",
      "Time: 2018-04-12 18:59:54.090374716~2018-04-12 19:14:54.959663182, Loss: 0.1898, Nodes_count: 170886, Cost Time: 359.26s\n",
      "Time: 2018-04-12 19:14:54.959663182~2018-04-12 19:29:55.160238546, Loss: 0.3328, Nodes_count: 171662, Cost Time: 361.03s\n",
      "Time: 2018-04-12 19:29:55.160238546~2018-04-12 19:44:55.907534312, Loss: 0.2873, Nodes_count: 172314, Cost Time: 362.75s\n",
      "Time: 2018-04-12 19:44:55.907534312~2018-04-12 19:59:56.089291407, Loss: 0.2881, Nodes_count: 173176, Cost Time: 364.54s\n",
      "Time: 2018-04-12 19:59:56.089291407~2018-04-12 20:14:56.869858929, Loss: 0.2746, Nodes_count: 174401, Cost Time: 367.69s\n",
      "Time: 2018-04-12 20:14:56.869858929~2018-04-12 20:30:33.002217462, Loss: 0.2962, Nodes_count: 175632, Cost Time: 370.45s\n",
      "Time: 2018-04-12 20:30:33.002217462~2018-04-12 20:45:41.109871080, Loss: 0.2685, Nodes_count: 176218, Cost Time: 371.82s\n",
      "Time: 2018-04-12 20:45:41.109871080~2018-04-12 21:01:57.550779236, Loss: 0.5392, Nodes_count: 177243, Cost Time: 374.28s\n",
      "Time: 2018-04-12 21:01:57.550779236~2018-04-12 21:17:05.931449051, Loss: 7.4801, Nodes_count: 320025, Cost Time: 402.52s\n",
      "Time: 2018-04-12 21:17:05.931449051~2018-04-12 21:32:59.560160630, Loss: 3.1372, Nodes_count: 380495, Cost Time: 420.31s\n",
      "Time: 2018-04-12 21:32:59.560160630~2018-04-12 21:48:00.113751799, Loss: 0.2960, Nodes_count: 381034, Cost Time: 422.66s\n",
      "Time: 2018-04-12 21:48:00.113751799~2018-04-12 22:03:13.001509745, Loss: 0.3181, Nodes_count: 382370, Cost Time: 425.86s\n",
      "Time: 2018-04-12 22:03:13.001509745~2018-04-12 22:19:00.880054693, Loss: 0.2880, Nodes_count: 382712, Cost Time: 427.13s\n",
      "Time: 2018-04-12 22:19:00.880054693~2018-04-12 22:34:01.001791765, Loss: 0.2696, Nodes_count: 383275, Cost Time: 428.78s\n",
      "Time: 2018-04-12 22:34:01.001791765~2018-04-12 22:49:06.001717261, Loss: 0.2609, Nodes_count: 383958, Cost Time: 430.86s\n",
      "Time: 2018-04-12 22:49:06.001717261~2018-04-12 23:04:45.002044287, Loss: 0.2737, Nodes_count: 385112, Cost Time: 433.68s\n",
      "Time: 2018-04-12 23:04:45.002044287~2018-04-12 23:19:53.001473944, Loss: 0.2922, Nodes_count: 385347, Cost Time: 434.51s\n",
      "Time: 2018-04-12 23:19:53.001473944~2018-04-12 23:34:55.001471636, Loss: 0.2311, Nodes_count: 385531, Cost Time: 435.19s\n",
      "Time: 2018-04-12 23:34:55.001471636~2018-04-12 23:51:06.307836193, Loss: 0.2662, Nodes_count: 385735, Cost Time: 435.92s\n"
     ]
    }
   ],
   "source": [
    "model=torch.load(\"./models/model_saved_emb100_BATCH_1024_LastAggregator_multiclass_without_neg_edge.pt\",map_location=device)\n",
    "memory,gnn, link_pred,neighbor_loader=model\n",
    "ans_4_12=test_day_new(graph_4_12,\"graph_4_12\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compute anomlous score and Initialize the node IDF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cal_train_IDF(find_str,file_list):\n",
    "    include_count=0\n",
    "    for f_path in (file_list):\n",
    "        f=open(f_path)\n",
    "        if find_str in f.read():\n",
    "            include_count+=1             \n",
    "    IDF=math.log(len(file_list)/(include_count+1))\n",
    "    return IDF\n",
    "\n",
    "\n",
    "def cal_IDF(find_str,file_path,file_list):\n",
    "    file_list=os.listdir(file_path)\n",
    "    include_count=0\n",
    "    different_neighbor=set()\n",
    "    for f_path in (file_list):\n",
    "        f=open(file_path+f_path)\n",
    "        if find_str in f.read():\n",
    "            include_count+=1\n",
    "#         add=True\n",
    "#         for line in f:\n",
    "            \n",
    "#             if find_str in line:\n",
    "# #                 print(line)\n",
    "#                 if add:\n",
    "#                     include_count+=1\n",
    "#                     add=False\n",
    "#                 l=line.strip()\n",
    "#                 jdata=eval(l)\n",
    "#                 different_neighbor.add(jdata['srcmsg'])\n",
    "#                 different_neighbor.add(jdata['dstmsg'])\n",
    "                \n",
    "                \n",
    "    IDF=math.log(len(file_list)/(include_count+1))\n",
    "    \n",
    "    return IDF,1\n",
    "\n",
    "def cal_IDF_by_file_in_mem(find_str,file_list):\n",
    "\n",
    "    include_count=0\n",
    "    different_neighbor=set()\n",
    "    for f_path in (file_list):\n",
    "        f=open(file_path+f_path)\n",
    "        if find_str in f.read():\n",
    "            include_count+=1\n",
    "#         add=True\n",
    "#         for line in f:\n",
    "            \n",
    "#             if find_str in line:\n",
    "# #                 print(line)\n",
    "#                 if add:\n",
    "#                     include_count+=1\n",
    "#                     add=False\n",
    "#                 l=line.strip()\n",
    "#                 jdata=eval(l)\n",
    "#                 different_neighbor.add(jdata['srcmsg'])\n",
    "#                 different_neighbor.add(jdata['dstmsg'])\n",
    "                \n",
    "                \n",
    "    IDF=math.log(len(file_list)/(include_count+1))\n",
    "    \n",
    "    return IDF,1\n",
    "\n",
    "def cal_redundant(find_str,edge_list):\n",
    "    \n",
    "    different_neighbor=set()\n",
    "    for e in edge_list:\n",
    "        if find_str in str(e):\n",
    "            different_neighbor.add(e[0])\n",
    "            different_neighbor.add(e[1])\n",
    "    return len(different_neighbor)-2\n",
    "\n",
    "def cal_anomaly_loss(loss_list,edge_list,file_path):\n",
    "    \n",
    "    if len(loss_list)!=len(edge_list):\n",
    "        print(\"error!\")\n",
    "        return 0\n",
    "    count=0\n",
    "    loss_sum=0\n",
    "    loss_std=std(loss_list)\n",
    "    loss_mean=mean(loss_list)\n",
    "    edge_set=set()\n",
    "    node_set=set()\n",
    "    node2redundant={}\n",
    "    \n",
    "    thr=loss_mean+1.5*loss_std\n",
    "\n",
    "    print(\"thr:\",thr)\n",
    "  \n",
    "    for i in range(len(loss_list)):\n",
    "        if loss_list[i]>thr:\n",
    "            count+=1\n",
    "            src_node=edge_list[i][0]\n",
    "            dst_node=edge_list[i][1]\n",
    "          \n",
    "            loss_sum+=loss_list[i]\n",
    "    \n",
    "            node_set.add(src_node)\n",
    "            node_set.add(dst_node)\n",
    "            edge_set.add(edge_list[i][0]+edge_list[i][1])\n",
    "    return count, loss_sum/(count+0.00000000001),node_set,edge_set\n",
    "#     return count, count/len(loss_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████| 194/194 [02:20<00:00,  1.38it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IDF weight calculate complete!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "node_IDF={}\n",
    "node_set=set()\n",
    "\n",
    "file_list=[]\n",
    "\n",
    "file_path=\"graph_4_3/\"\n",
    "file_l=os.listdir(\"graph_4_3/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "\n",
    "file_path=\"graph_4_4/\"\n",
    "file_l=os.listdir(\"graph_4_4/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "\n",
    "file_path=\"graph_4_5/\"\n",
    "file_l=os.listdir(\"graph_4_5/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "\n",
    "    \n",
    "node_set = {}\n",
    "for f_path in tqdm(file_list):\n",
    "    f=open(f_path)\n",
    "    for line in f:\n",
    "        l=line.strip()\n",
    "        jdata=eval(l)\n",
    "        if jdata['loss']>0:\n",
    "            if 'netflow' not in str(jdata['srcmsg']) or True:\n",
    "                if str(jdata['srcmsg']) not in node_set.keys():\n",
    "                    node_set[str(jdata['srcmsg'])] = set([f_path])\n",
    "                else:\n",
    "                    node_set[str(jdata['srcmsg'])].add(f_path)\n",
    "            if 'netflow' not in str(jdata['dstmsg']) or True:\n",
    "                if str(jdata['dstmsg']) not in node_set.keys():\n",
    "                    node_set[str(jdata['dstmsg'])] = set([f_path])\n",
    "                else:\n",
    "                    node_set[str(jdata['dstmsg'])].add(f_path)\n",
    "for n in node_set:\n",
    "    include_count = len(node_set[n])   \n",
    "    IDF=math.log(len(file_list)/(include_count+1))\n",
    "    node_IDF[n] = IDF \n",
    "\n",
    "\n",
    "torch.save(node_IDF,\"node_IDF_4_3-5\")\n",
    "print(\"IDF weight calculate complete!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████| 43/43 [00:59<00:00,  1.38s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IDF weight calculate complete!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "node_IDF={}\n",
    "node_set=set()\n",
    "\n",
    "file_list=[]\n",
    "\n",
    "file_path=\"graph_4_10/\"\n",
    "file_l=os.listdir(\"graph_4_10/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "\n",
    "\n",
    "node_set = {}\n",
    "for f_path in tqdm(file_list):\n",
    "    f=open(f_path)\n",
    "    for line in f:\n",
    "        l=line.strip()\n",
    "        jdata=eval(l)\n",
    "        if jdata['loss']>0:\n",
    "            if 'netflow' not in str(jdata['srcmsg']) or True:\n",
    "                if str(jdata['srcmsg']) not in node_set.keys():\n",
    "                    node_set[str(jdata['srcmsg'])] = set([f_path])\n",
    "                else:\n",
    "                    node_set[str(jdata['srcmsg'])].add(f_path)\n",
    "            if 'netflow' not in str(jdata['dstmsg']) or True:\n",
    "                if str(jdata['dstmsg']) not in node_set.keys():\n",
    "                    node_set[str(jdata['dstmsg'])] = set([f_path])\n",
    "                else:\n",
    "                    node_set[str(jdata['dstmsg'])].add(f_path)\n",
    "for n in node_set:\n",
    "    include_count = len(node_set[n])   \n",
    "    IDF=math.log(len(file_list)/(include_count+1))\n",
    "    node_IDF[n] = IDF \n",
    "    \n",
    "\n",
    "torch.save(node_IDF,\"node_IDF_4_10\")\n",
    "print(\"IDF weight calculate complete!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████| 91/91 [01:10<00:00,  1.29it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IDF weight calculate complete!\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "node_IDF={}\n",
    "node_set=set()\n",
    "\n",
    "file_list=[]\n",
    "\n",
    "file_path=\"graph_4_11/\"\n",
    "file_l=os.listdir(\"graph_4_11/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "\n",
    "\n",
    "node_set = {}\n",
    "for f_path in tqdm(file_list):\n",
    "    f=open(f_path)\n",
    "    for line in f:\n",
    "        l=line.strip()\n",
    "        jdata=eval(l)\n",
    "        if jdata['loss']>0:\n",
    "            if 'netflow' not in str(jdata['srcmsg']) or True:\n",
    "                if str(jdata['srcmsg']) not in node_set.keys():\n",
    "                    node_set[str(jdata['srcmsg'])] = set([f_path])\n",
    "                else:\n",
    "                    node_set[str(jdata['srcmsg'])].add(f_path)\n",
    "            if 'netflow' not in str(jdata['dstmsg']) or True:\n",
    "                if str(jdata['dstmsg']) not in node_set.keys():\n",
    "                    node_set[str(jdata['dstmsg'])] = set([f_path])\n",
    "                else:\n",
    "                    node_set[str(jdata['dstmsg'])].add(f_path)\n",
    "for n in node_set:\n",
    "    include_count = len(node_set[n])   \n",
    "    IDF=math.log(len(file_list)/(include_count+1))\n",
    "    node_IDF[n] = IDF \n",
    "\n",
    "\n",
    "torch.save(node_IDF,\"node_IDF_4_11\")\n",
    "print(\"IDF weight calculate complete!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████| 93/93 [01:07<00:00,  1.37it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IDF weight calculate complete!\n"
     ]
    }
   ],
   "source": [
    "node_IDF={}\n",
    "node_set=set()\n",
    "\n",
    "file_list=[]\n",
    "\n",
    "file_path=\"graph_4_12/\"\n",
    "file_l=os.listdir(\"graph_4_12/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "\n",
    "\n",
    "node_set = {}\n",
    "for f_path in tqdm(file_list):\n",
    "    f=open(f_path)\n",
    "    for line in f:\n",
    "        l=line.strip()\n",
    "        jdata=eval(l)\n",
    "        if jdata['loss']>0:\n",
    "            if 'netflow' not in str(jdata['srcmsg']) or True:\n",
    "                if str(jdata['srcmsg']) not in node_set.keys():\n",
    "                    node_set[str(jdata['srcmsg'])] = set([f_path])\n",
    "                else:\n",
    "                    node_set[str(jdata['srcmsg'])].add(f_path)\n",
    "            if 'netflow' not in str(jdata['dstmsg']) or True:\n",
    "                if str(jdata['dstmsg']) not in node_set.keys():\n",
    "                    node_set[str(jdata['dstmsg'])] = set([f_path])\n",
    "                else:\n",
    "                    node_set[str(jdata['dstmsg'])].add(f_path)\n",
    "for n in node_set:\n",
    "    include_count = len(node_set[n])   \n",
    "    IDF=math.log(len(file_list)/(include_count+1))\n",
    "    node_IDF[n] = IDF \n",
    "    \n",
    "\n",
    "torch.save(node_IDF,\"node_IDF_4_12\")\n",
    "print(\"IDF weight calculate complete!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Construct the relations between time windows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4-10,11\n",
    "def is_include_key_word_bak(s):\n",
    "    keywords=[\n",
    "         'netflow',\n",
    "        'null',\n",
    "        '/dev/pts',\n",
    "        'salt-minion.log',\n",
    "        '675',\n",
    "        'usr',\n",
    "         'proc',\n",
    "        '/.cache/mozilla/',\n",
    "        'tmp',\n",
    "        'thunderbird',\n",
    "        '/bin/',\n",
    "        '/sbin/sysctl',\n",
    "        '/data/replay_logdb/',\n",
    "        '/home/admin/eraseme',\n",
    "        \n",
    "        '/stat',\n",
    "        \n",
    "      ]\n",
    "    flag=False\n",
    "    for i in keywords:\n",
    "        if i in s:\n",
    "            flag=True\n",
    "    return flag\n",
    "\n",
    "\n",
    "def cal_set_rel_bak(s1,s2,file_list):\n",
    "    new_s=s1 & s2\n",
    "    count=0\n",
    "    for i in new_s:\n",
    "#     jdata=json.loads(i)\n",
    "        if is_include_key_word_bak(i) is not True:\n",
    "            if i in node_IDF.keys():\n",
    "                IDF=node_IDF[i]\n",
    "            else:\n",
    "                IDF=math.log(len(file_list)/(1))         \n",
    "\n",
    "            if (IDF)>math.log(len(file_list)*0.9/(1))  :\n",
    "                print(\"node:\",i,\" IDF:\",IDF)\n",
    "                count+=1\n",
    "    return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4-12\n",
    "\n",
    "# def is_include_key_word_bak(s):\n",
    "#     keywords=[\n",
    "#          'netflow',        \n",
    "#         '/dev/pts',\n",
    "#         'salt-minion.log',\n",
    "#         'null',\n",
    "#         'usr',\n",
    "#          'proc',\n",
    "#         'firefox',\n",
    "#         'tmp',\n",
    "#         'thunderbird',\n",
    "#         'bin/',\n",
    "#         '/data/replay_logdb',\n",
    "#         '/stat',\n",
    "#         '/boot',\n",
    "#         'qt-opensource-linux-x64',\n",
    "#         '/eraseme',\n",
    "#         '675',\n",
    "        \n",
    "# #       \n",
    "# #         'etc',  \n",
    "# #         'cdrom', \n",
    "# #         'shm'\n",
    "#       ]\n",
    "#     flag=False\n",
    "#     for i in keywords:\n",
    "#         if i in s:\n",
    "#             flag=True\n",
    "#     return flag\n",
    "\n",
    "# def cal_set_rel_bak(s1,s2,file_list):\n",
    "#     new_s=s1 & s2\n",
    "#     count=0\n",
    "#     for i in new_s:\n",
    "# #     jdata=json.loads(i)\n",
    "#         if is_include_key_word_bak(i) is not True:\n",
    "#             if i in node_IDF.keys():\n",
    "#                 IDF=node_IDF[i]\n",
    "#             else:\n",
    "#                 IDF=math.log(len(file_list)/(1))         \n",
    "\n",
    "#             if (IDF)>math.log(len(file_list)*0.9/(1))  :\n",
    "#                 print(\"node:\",i,\" IDF:\",IDF)\n",
    "#                 count+=1\n",
    "#     return count\n",
    "\n",
    "\n",
    "def is_include_key_word(s):\n",
    "    keywords=[\n",
    "         'netflow',        \n",
    "        '/dev/pts',\n",
    "        'salt-minion.log',\n",
    "        'null',\n",
    "        'usr',\n",
    "         'proc',\n",
    "        'firefox',\n",
    "        'tmp',\n",
    "        'thunderbird',\n",
    "        'bin/',\n",
    "        '/data/replay_logdb',\n",
    "        '/stat',\n",
    "        '/boot',\n",
    "        'qt-opensource-linux-x64',\n",
    "        '/eraseme',\n",
    "        '675',\n",
    "      ]\n",
    "    flag=False\n",
    "    for i in keywords:\n",
    "        if i in s:\n",
    "            flag=True\n",
    "    return flag\n",
    "\n",
    "\n",
    "file_list=[]\n",
    "\n",
    "file_path=\"graph_4_12/\"\n",
    "file_l=os.listdir(\"graph_4_12/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "    \n",
    "\n",
    "\n",
    "def cal_set_rel(s1,s2,file_list, file_list_4_3_5):\n",
    "    IDF3 = node_IDF_3\n",
    "    new_s=s1 & s2\n",
    "    count=0\n",
    "    for i in new_s:\n",
    "#     jdata=json.loads(i)\n",
    "       if is_include_key_word(i) is not True:\n",
    "        \n",
    "#         'netflow' not in i\n",
    "#         and 'usr' not in i and 'var' not in i\n",
    "            if i in node_IDF.keys():\n",
    "                IDF=node_IDF[i]\n",
    "            else:\n",
    "                IDF=math.log(len(file_list)/(1))\n",
    "                \n",
    "            if i in node_IDF_3.keys():\n",
    "                IDF3=node_IDF_3[i]\n",
    "            else:\n",
    "                IDF3=math.log(len(file_list_4_3_5)/(1))    \n",
    "            \n",
    "#             print(IDF)\n",
    "            if (IDF+IDF3)>5 :\n",
    "                print(\"node:\",i,\" IDF:\",IDF)\n",
    "                count+=1\n",
    "    return count"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# label generation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels={}\n",
    "\n",
    "    \n",
    "filelist = os.listdir(\"graph_4_10/\")\n",
    "for f in filelist:\n",
    "    labels[\"graph_4_10/\"+f]=0\n",
    "\n",
    "filelist = os.listdir(\"graph_4_11/\")\n",
    "for f in filelist:\n",
    "    labels[\"graph_4_11/\"+f]=0\n",
    "    \n",
    "filelist = os.listdir(\"graph_4_12/\")\n",
    "for f in filelist:\n",
    "    labels[\"graph_4_12/\"+f]=0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['graph_4_10/2018-04-10 12:44:33.449564893~2018-04-10 13:00:02.700560774.txt',\n",
       " 'graph_4_10/2018-04-10 13:00:02.700560774~2018-04-10 13:16:13.551944728.txt',\n",
       " 'graph_4_10/2018-04-10 13:16:13.551944728~2018-04-10 13:31:14.548738409.txt',\n",
       " 'graph_4_10/2018-04-10 13:31:14.548738409~2018-04-10 13:46:36.161065223.txt',\n",
       " 'graph_4_10/2018-04-10 13:46:36.161065223~2018-04-10 14:02:17.001271389.txt',\n",
       " 'graph_4_10/2018-04-10 14:02:17.001271389~2018-04-10 14:17:34.001373488.txt',\n",
       " 'graph_4_10/2018-04-10 14:17:34.001373488~2018-04-10 14:33:18.350772859.txt',\n",
       " 'graph_4_10/2018-04-10 14:33:18.350772859~2018-04-10 14:48:47.320442910.txt',\n",
       " 'graph_4_10/2018-04-10 14:48:47.320442910~2018-04-10 15:03:54.307022037.txt',\n",
       " 'graph_4_10/2018-04-10 15:03:54.307022037~2018-04-10 15:19:25.001773315.txt',\n",
       " 'graph_4_10/2018-04-10 15:19:25.001773315~2018-04-10 15:36:13.002273705.txt',\n",
       " 'graph_4_10/2018-04-10 15:36:13.002273705~2018-04-10 15:51:24.614585595.txt',\n",
       " 'graph_4_10/2018-04-10 15:51:24.614585595~2018-04-10 16:06:34.924760399.txt',\n",
       " 'graph_4_10/2018-04-10 16:06:34.924760399~2018-04-10 16:21:58.949085533.txt',\n",
       " 'graph_4_10/2018-04-10 16:21:58.949085533~2018-04-10 16:37:32.001491870.txt',\n",
       " 'graph_4_10/2018-04-10 16:37:32.001491870~2018-04-10 16:52:38.298464794.txt',\n",
       " 'graph_4_10/2018-04-10 16:52:38.298464794~2018-04-10 17:07:45.640852929.txt',\n",
       " 'graph_4_10/2018-04-10 17:07:45.640852929~2018-04-10 17:22:45.926569237.txt',\n",
       " 'graph_4_10/2018-04-10 17:22:45.926569237~2018-04-10 17:37:49.534629845.txt',\n",
       " 'graph_4_10/2018-04-10 17:37:49.534629845~2018-04-10 17:52:50.724731198.txt',\n",
       " 'graph_4_10/2018-04-10 17:52:50.724731198~2018-04-10 18:08:02.001452859.txt',\n",
       " 'graph_4_10/2018-04-10 18:08:02.001452859~2018-04-10 18:23:10.812709243.txt',\n",
       " 'graph_4_10/2018-04-10 18:23:10.812709243~2018-04-10 18:38:10.882714584.txt',\n",
       " 'graph_4_10/2018-04-10 18:38:10.882714584~2018-04-10 18:54:51.062762969.txt',\n",
       " 'graph_4_10/2018-04-10 18:54:51.062762969~2018-04-10 19:10:03.757528839.txt',\n",
       " 'graph_4_10/2018-04-10 19:10:03.757528839~2018-04-10 19:26:29.001922190.txt',\n",
       " 'graph_4_10/2018-04-10 19:26:29.001922190~2018-04-10 19:41:52.912938380.txt',\n",
       " 'graph_4_10/2018-04-10 19:41:52.912938380~2018-04-10 19:57:35.001557519.txt',\n",
       " 'graph_4_10/2018-04-10 19:57:35.001557519~2018-04-10 20:13:24.002153177.txt',\n",
       " 'graph_4_10/2018-04-10 20:13:24.002153177~2018-04-10 20:29:32.050293487.txt',\n",
       " 'graph_4_10/2018-04-10 20:29:32.050293487~2018-04-10 20:44:46.874595566.txt',\n",
       " 'graph_4_10/2018-04-10 20:44:46.874595566~2018-04-10 21:00:56.078860802.txt',\n",
       " 'graph_4_10/2018-04-10 21:00:56.078860802~2018-04-10 21:16:35.002073797.txt',\n",
       " 'graph_4_10/2018-04-10 21:16:35.002073797~2018-04-10 21:32:59.002389345.txt',\n",
       " 'graph_4_10/2018-04-10 21:32:59.002389345~2018-04-10 21:48:01.438127726.txt',\n",
       " 'graph_4_10/2018-04-10 21:48:01.438127726~2018-04-10 22:03:05.222735201.txt',\n",
       " 'graph_4_10/2018-04-10 22:03:05.222735201~2018-04-10 22:18:56.001694554.txt',\n",
       " 'graph_4_10/2018-04-10 22:18:56.001694554~2018-04-10 22:35:47.001550655.txt',\n",
       " 'graph_4_10/2018-04-10 22:35:47.001550655~2018-04-10 22:51:30.001246535.txt',\n",
       " 'graph_4_10/2018-04-10 22:51:30.001246535~2018-04-10 23:07:01.106168658.txt',\n",
       " 'graph_4_10/2018-04-10 23:07:01.106168658~2018-04-10 23:22:23.515914390.txt',\n",
       " 'graph_4_10/2018-04-10 23:22:23.515914390~2018-04-10 23:38:02.289791440.txt',\n",
       " 'graph_4_10/2018-04-10 23:38:02.289791440~2018-04-10 23:53:53.001127888.txt',\n",
       " 'graph_4_11/2018-04-11 00:00:00.001151329~2018-04-11 00:16:06.001274623.txt',\n",
       " 'graph_4_11/2018-04-11 00:16:06.001274623~2018-04-11 00:32:08.001169192.txt',\n",
       " 'graph_4_11/2018-04-11 00:32:08.001169192~2018-04-11 00:48:31.001594545.txt',\n",
       " 'graph_4_11/2018-04-11 00:48:31.001594545~2018-04-11 01:04:53.001888013.txt',\n",
       " 'graph_4_11/2018-04-11 01:04:53.001888013~2018-04-11 01:20:59.002307553.txt',\n",
       " 'graph_4_11/2018-04-11 01:20:59.002307553~2018-04-11 01:37:06.826246565.txt',\n",
       " 'graph_4_11/2018-04-11 01:37:06.826246565~2018-04-11 01:53:33.001963923.txt',\n",
       " 'graph_4_11/2018-04-11 01:53:33.001963923~2018-04-11 02:10:00.001194307.txt',\n",
       " 'graph_4_11/2018-04-11 02:10:00.001194307~2018-04-11 02:26:03.001337306.txt',\n",
       " 'graph_4_11/2018-04-11 02:26:03.001337306~2018-04-11 02:42:12.001881387.txt',\n",
       " 'graph_4_11/2018-04-11 02:42:12.001881387~2018-04-11 02:58:40.001723965.txt',\n",
       " 'graph_4_11/2018-04-11 02:58:40.001723965~2018-04-11 03:15:01.001706699.txt',\n",
       " 'graph_4_11/2018-04-11 03:15:01.001706699~2018-04-11 03:31:06.001379178.txt',\n",
       " 'graph_4_11/2018-04-11 03:31:06.001379178~2018-04-11 03:47:16.001779251.txt',\n",
       " 'graph_4_11/2018-04-11 03:47:16.001779251~2018-04-11 04:03:26.001627450.txt',\n",
       " 'graph_4_11/2018-04-11 04:03:26.001627450~2018-04-11 04:19:33.002034536.txt',\n",
       " 'graph_4_11/2018-04-11 04:19:33.002034536~2018-04-11 04:35:40.001832776.txt',\n",
       " 'graph_4_11/2018-04-11 04:35:40.001832776~2018-04-11 04:52:02.636356915.txt',\n",
       " 'graph_4_11/2018-04-11 04:52:02.636356915~2018-04-11 05:08:14.807062185.txt',\n",
       " 'graph_4_11/2018-04-11 05:08:14.807062185~2018-04-11 05:24:22.001019938.txt',\n",
       " 'graph_4_11/2018-04-11 05:24:22.001019938~2018-04-11 05:40:42.002091762.txt',\n",
       " 'graph_4_11/2018-04-11 05:40:42.002091762~2018-04-11 05:57:05.001981906.txt',\n",
       " 'graph_4_11/2018-04-11 05:57:05.001981906~2018-04-11 06:13:20.001263471.txt',\n",
       " 'graph_4_11/2018-04-11 06:13:20.001263471~2018-04-11 06:29:17.894355498.txt',\n",
       " 'graph_4_11/2018-04-11 06:29:17.894355498~2018-04-11 06:45:36.001432729.txt',\n",
       " 'graph_4_11/2018-04-11 06:45:36.001432729~2018-04-11 07:02:01.002206348.txt',\n",
       " 'graph_4_11/2018-04-11 07:02:01.002206348~2018-04-11 07:18:14.001812105.txt',\n",
       " 'graph_4_11/2018-04-11 07:18:14.001812105~2018-04-11 07:34:30.001240773.txt',\n",
       " 'graph_4_11/2018-04-11 07:34:30.001240773~2018-04-11 07:49:43.130898948.txt',\n",
       " 'graph_4_11/2018-04-11 07:49:43.130898948~2018-04-11 08:05:01.864920978.txt',\n",
       " 'graph_4_11/2018-04-11 08:05:01.864920978~2018-04-11 08:20:03.240565286.txt',\n",
       " 'graph_4_11/2018-04-11 08:20:03.240565286~2018-04-11 08:35:12.182436041.txt',\n",
       " 'graph_4_11/2018-04-11 08:35:12.182436041~2018-04-11 08:51:45.001713561.txt',\n",
       " 'graph_4_11/2018-04-11 08:51:45.001713561~2018-04-11 09:07:41.001516635.txt',\n",
       " 'graph_4_11/2018-04-11 09:07:41.001516635~2018-04-11 09:23:47.001103293.txt',\n",
       " 'graph_4_11/2018-04-11 09:23:47.001103293~2018-04-11 09:38:59.728361285.txt',\n",
       " 'graph_4_11/2018-04-11 09:38:59.728361285~2018-04-11 09:54:01.833145793.txt',\n",
       " 'graph_4_11/2018-04-11 09:54:01.833145793~2018-04-11 10:09:03.508820548.txt',\n",
       " 'graph_4_11/2018-04-11 10:09:03.508820548~2018-04-11 10:24:19.673656595.txt',\n",
       " 'graph_4_11/2018-04-11 10:24:19.673656595~2018-04-11 10:39:40.241867510.txt',\n",
       " 'graph_4_11/2018-04-11 10:39:40.241867510~2018-04-11 10:56:00.001497677.txt',\n",
       " 'graph_4_11/2018-04-11 10:56:00.001497677~2018-04-11 11:11:11.539494920.txt',\n",
       " 'graph_4_11/2018-04-11 11:11:11.539494920~2018-04-11 11:26:42.586327848.txt',\n",
       " 'graph_4_11/2018-04-11 11:26:42.586327848~2018-04-11 11:41:54.164805680.txt',\n",
       " 'graph_4_11/2018-04-11 11:41:54.164805680~2018-04-11 11:57:06.982513263.txt',\n",
       " 'graph_4_11/2018-04-11 11:57:06.982513263~2018-04-11 12:13:41.616470541.txt',\n",
       " 'graph_4_11/2018-04-11 12:13:41.616470541~2018-04-11 12:29:18.001334304.txt',\n",
       " 'graph_4_11/2018-04-11 12:29:18.001334304~2018-04-11 12:44:47.002346666.txt',\n",
       " 'graph_4_11/2018-04-11 12:44:47.002346666~2018-04-11 13:01:13.002135659.txt',\n",
       " 'graph_4_11/2018-04-11 13:01:13.002135659~2018-04-11 13:16:50.021059784.txt',\n",
       " 'graph_4_11/2018-04-11 13:16:50.021059784~2018-04-11 13:31:56.533212332.txt',\n",
       " 'graph_4_11/2018-04-11 13:31:56.533212332~2018-04-11 13:46:57.298929583.txt',\n",
       " 'graph_4_11/2018-04-11 13:46:57.298929583~2018-04-11 14:02:03.185872207.txt',\n",
       " 'graph_4_11/2018-04-11 14:02:03.185872207~2018-04-11 14:17:10.448756073.txt',\n",
       " 'graph_4_11/2018-04-11 14:17:10.448756073~2018-04-11 14:32:13.526157650.txt',\n",
       " 'graph_4_11/2018-04-11 14:32:13.526157650~2018-04-11 14:47:23.351053223.txt',\n",
       " 'graph_4_11/2018-04-11 14:47:23.351053223~2018-04-11 15:03:33.590152518.txt',\n",
       " 'graph_4_11/2018-04-11 15:03:33.590152518~2018-04-11 15:20:05.001754985.txt',\n",
       " 'graph_4_11/2018-04-11 15:20:05.001754985~2018-04-11 15:36:18.002102989.txt',\n",
       " 'graph_4_11/2018-04-11 15:36:18.002102989~2018-04-11 15:52:42.862552619.txt',\n",
       " 'graph_4_11/2018-04-11 15:52:42.862552619~2018-04-11 16:08:52.001653616.txt',\n",
       " 'graph_4_11/2018-04-11 16:08:52.001653616~2018-04-11 16:23:53.622260753.txt',\n",
       " 'graph_4_11/2018-04-11 16:23:53.622260753~2018-04-11 16:40:20.001018770.txt',\n",
       " 'graph_4_11/2018-04-11 16:40:20.001018770~2018-04-11 16:55:20.537127474.txt',\n",
       " 'graph_4_11/2018-04-11 16:55:20.537127474~2018-04-11 17:11:02.002108596.txt',\n",
       " 'graph_4_11/2018-04-11 17:11:02.002108596~2018-04-11 17:26:02.530643144.txt',\n",
       " 'graph_4_11/2018-04-11 17:26:02.530643144~2018-04-11 17:41:17.001646675.txt',\n",
       " 'graph_4_11/2018-04-11 17:41:17.001646675~2018-04-11 17:56:19.699660881.txt',\n",
       " 'graph_4_11/2018-04-11 17:56:19.699660881~2018-04-11 18:11:46.356548226.txt',\n",
       " 'graph_4_11/2018-04-11 18:11:46.356548226~2018-04-11 18:26:53.001667593.txt',\n",
       " 'graph_4_11/2018-04-11 18:26:53.001667593~2018-04-11 18:42:17.001965855.txt',\n",
       " 'graph_4_11/2018-04-11 18:42:17.001965855~2018-04-11 18:58:43.001951167.txt',\n",
       " 'graph_4_11/2018-04-11 18:58:43.001951167~2018-04-11 19:14:49.001363010.txt',\n",
       " 'graph_4_11/2018-04-11 19:14:49.001363010~2018-04-11 19:30:09.001780046.txt',\n",
       " 'graph_4_11/2018-04-11 19:30:09.001780046~2018-04-11 19:46:28.001487257.txt',\n",
       " 'graph_4_11/2018-04-11 19:46:28.001487257~2018-04-11 20:02:07.594306306.txt',\n",
       " 'graph_4_11/2018-04-11 20:02:07.594306306~2018-04-11 20:17:10.001507207.txt',\n",
       " 'graph_4_11/2018-04-11 20:17:10.001507207~2018-04-11 20:33:47.001341878.txt',\n",
       " 'graph_4_11/2018-04-11 20:33:47.001341878~2018-04-11 20:48:52.002055784.txt',\n",
       " 'graph_4_11/2018-04-11 20:48:52.002055784~2018-04-11 21:03:56.946916791.txt',\n",
       " 'graph_4_11/2018-04-11 21:03:56.946916791~2018-04-11 21:20:04.001983239.txt',\n",
       " 'graph_4_11/2018-04-11 21:20:04.001983239~2018-04-11 21:36:36.001464315.txt',\n",
       " 'graph_4_11/2018-04-11 21:36:36.001464315~2018-04-11 21:53:05.001134741.txt',\n",
       " 'graph_4_11/2018-04-11 21:53:05.001134741~2018-04-11 22:08:43.001339416.txt',\n",
       " 'graph_4_11/2018-04-11 22:08:43.001339416~2018-04-11 22:24:17.001832396.txt',\n",
       " 'graph_4_11/2018-04-11 22:24:17.001832396~2018-04-11 22:39:54.002070033.txt',\n",
       " 'graph_4_11/2018-04-11 22:39:54.002070033~2018-04-11 22:56:00.892549695.txt',\n",
       " 'graph_4_11/2018-04-11 22:56:00.892549695~2018-04-11 23:11:03.001690405.txt',\n",
       " 'graph_4_11/2018-04-11 23:11:03.001690405~2018-04-11 23:26:56.002055798.txt',\n",
       " 'graph_4_11/2018-04-11 23:26:56.002055798~2018-04-11 23:43:02.705859964.txt',\n",
       " 'graph_4_11/2018-04-11 23:43:02.705859964~2018-04-11 23:59:03.355632261.txt',\n",
       " 'graph_4_12/2018-04-12 00:00:00.001773757~2018-04-12 00:15:26.001647081.txt',\n",
       " 'graph_4_12/2018-04-12 00:15:26.001647081~2018-04-12 00:30:58.002002412.txt',\n",
       " 'graph_4_12/2018-04-12 00:30:58.002002412~2018-04-12 00:46:27.001835122.txt',\n",
       " 'graph_4_12/2018-04-12 00:46:27.001835122~2018-04-12 01:02:05.698953597.txt',\n",
       " 'graph_4_12/2018-04-12 01:02:05.698953597~2018-04-12 01:17:27.002277528.txt',\n",
       " 'graph_4_12/2018-04-12 01:17:27.002277528~2018-04-12 01:32:48.001732365.txt',\n",
       " 'graph_4_12/2018-04-12 01:32:48.001732365~2018-04-12 01:48:20.001593370.txt',\n",
       " 'graph_4_12/2018-04-12 01:48:20.001593370~2018-04-12 02:04:08.001895394.txt',\n",
       " 'graph_4_12/2018-04-12 02:04:08.001895394~2018-04-12 02:19:33.000943880.txt',\n",
       " 'graph_4_12/2018-04-12 02:19:33.000943880~2018-04-12 02:35:09.219021781.txt',\n",
       " 'graph_4_12/2018-04-12 02:35:09.219021781~2018-04-12 02:50:55.001941925.txt',\n",
       " 'graph_4_12/2018-04-12 02:50:55.001941925~2018-04-12 03:06:27.001769928.txt',\n",
       " 'graph_4_12/2018-04-12 03:06:27.001769928~2018-04-12 03:22:01.001281628.txt',\n",
       " 'graph_4_12/2018-04-12 03:22:01.001281628~2018-04-12 03:37:29.838784853.txt',\n",
       " 'graph_4_12/2018-04-12 03:37:29.838784853~2018-04-12 03:53:07.001316649.txt',\n",
       " 'graph_4_12/2018-04-12 03:53:07.001316649~2018-04-12 04:08:50.001184547.txt',\n",
       " 'graph_4_12/2018-04-12 04:08:50.001184547~2018-04-12 04:24:00.001592464.txt',\n",
       " 'graph_4_12/2018-04-12 04:24:00.001592464~2018-04-12 04:39:26.002162640.txt',\n",
       " 'graph_4_12/2018-04-12 04:39:26.002162640~2018-04-12 04:55:10.001772049.txt',\n",
       " 'graph_4_12/2018-04-12 04:55:10.001772049~2018-04-12 05:10:51.001908824.txt',\n",
       " 'graph_4_12/2018-04-12 05:10:51.001908824~2018-04-12 05:26:15.658033392.txt',\n",
       " 'graph_4_12/2018-04-12 05:26:15.658033392~2018-04-12 05:41:53.000960686.txt',\n",
       " 'graph_4_12/2018-04-12 05:41:53.000960686~2018-04-12 05:57:26.001519319.txt',\n",
       " 'graph_4_12/2018-04-12 05:57:26.001519319~2018-04-12 06:13:11.001365623.txt',\n",
       " 'graph_4_12/2018-04-12 06:13:11.001365623~2018-04-12 06:28:17.995464541.txt',\n",
       " 'graph_4_12/2018-04-12 06:28:17.995464541~2018-04-12 06:44:00.001985599.txt',\n",
       " 'graph_4_12/2018-04-12 06:44:00.001985599~2018-04-12 06:59:19.188577225.txt',\n",
       " 'graph_4_12/2018-04-12 06:59:19.188577225~2018-04-12 07:14:23.001990475.txt',\n",
       " 'graph_4_12/2018-04-12 07:14:23.001990475~2018-04-12 07:29:24.250402681.txt',\n",
       " 'graph_4_12/2018-04-12 07:29:24.250402681~2018-04-12 07:46:06.002011766.txt',\n",
       " 'graph_4_12/2018-04-12 07:46:06.002011766~2018-04-12 08:02:07.001316523.txt',\n",
       " 'graph_4_12/2018-04-12 08:02:07.001316523~2018-04-12 08:17:47.001755201.txt',\n",
       " 'graph_4_12/2018-04-12 08:17:47.001755201~2018-04-12 08:33:06.483036511.txt',\n",
       " 'graph_4_12/2018-04-12 08:33:06.483036511~2018-04-12 08:48:09.600264510.txt',\n",
       " 'graph_4_12/2018-04-12 08:48:09.600264510~2018-04-12 09:03:50.002384931.txt',\n",
       " 'graph_4_12/2018-04-12 09:03:50.002384931~2018-04-12 09:19:03.668714727.txt',\n",
       " 'graph_4_12/2018-04-12 09:19:03.668714727~2018-04-12 09:34:16.330657912.txt',\n",
       " 'graph_4_12/2018-04-12 09:34:16.330657912~2018-04-12 09:49:24.841744252.txt',\n",
       " 'graph_4_12/2018-04-12 09:49:24.841744252~2018-04-12 10:04:31.967749880.txt',\n",
       " 'graph_4_12/2018-04-12 10:04:31.967749880~2018-04-12 10:19:41.864196505.txt',\n",
       " 'graph_4_12/2018-04-12 10:19:41.864196505~2018-04-12 10:34:55.515804775.txt',\n",
       " 'graph_4_12/2018-04-12 10:34:55.515804775~2018-04-12 10:50:58.001487795.txt',\n",
       " 'graph_4_12/2018-04-12 10:50:58.001487795~2018-04-12 11:06:07.082292087.txt',\n",
       " 'graph_4_12/2018-04-12 11:06:07.082292087~2018-04-12 11:22:02.614075429.txt',\n",
       " 'graph_4_12/2018-04-12 11:22:02.614075429~2018-04-12 11:38:02.721265247.txt',\n",
       " 'graph_4_12/2018-04-12 11:38:02.721265247~2018-04-12 11:53:05.002139204.txt',\n",
       " 'graph_4_12/2018-04-12 11:53:05.002139204~2018-04-12 12:08:50.001259045.txt',\n",
       " 'graph_4_12/2018-04-12 12:08:50.001259045~2018-04-12 12:24:03.925533802.txt',\n",
       " 'graph_4_12/2018-04-12 12:24:03.925533802~2018-04-12 12:39:06.592684498.txt',\n",
       " 'graph_4_12/2018-04-12 12:39:06.592684498~2018-04-12 12:54:44.001888457.txt',\n",
       " 'graph_4_12/2018-04-12 12:54:44.001888457~2018-04-12 13:09:55.026832462.txt',\n",
       " 'graph_4_12/2018-04-12 13:09:55.026832462~2018-04-12 13:25:06.588370709.txt',\n",
       " 'graph_4_12/2018-04-12 13:25:06.588370709~2018-04-12 13:40:07.178206094.txt',\n",
       " 'graph_4_12/2018-04-12 13:40:07.178206094~2018-04-12 13:56:02.001338519.txt',\n",
       " 'graph_4_12/2018-04-12 13:56:02.001338519~2018-04-12 14:11:10.272552800.txt',\n",
       " 'graph_4_12/2018-04-12 14:11:10.272552800~2018-04-12 14:26:13.739235539.txt',\n",
       " 'graph_4_12/2018-04-12 14:26:13.739235539~2018-04-12 14:41:15.795737707.txt',\n",
       " 'graph_4_12/2018-04-12 14:41:15.795737707~2018-04-12 14:56:26.556444597.txt',\n",
       " 'graph_4_12/2018-04-12 14:56:26.556444597~2018-04-12 15:11:30.532680843.txt',\n",
       " 'graph_4_12/2018-04-12 15:11:30.532680843~2018-04-12 15:26:31.776115575.txt',\n",
       " 'graph_4_12/2018-04-12 15:26:31.776115575~2018-04-12 15:41:41.913691938.txt',\n",
       " 'graph_4_12/2018-04-12 15:41:41.913691938~2018-04-12 15:56:42.439764347.txt',\n",
       " 'graph_4_12/2018-04-12 15:56:42.439764347~2018-04-12 16:11:42.990798525.txt',\n",
       " 'graph_4_12/2018-04-12 16:11:42.990798525~2018-04-12 16:26:43.075609426.txt',\n",
       " 'graph_4_12/2018-04-12 16:26:43.075609426~2018-04-12 16:42:02.002013322.txt',\n",
       " 'graph_4_12/2018-04-12 16:42:02.002013322~2018-04-12 16:58:02.604248444.txt',\n",
       " 'graph_4_12/2018-04-12 16:58:02.604248444~2018-04-12 17:13:29.514953544.txt',\n",
       " 'graph_4_12/2018-04-12 17:13:29.514953544~2018-04-12 17:28:37.568314356.txt',\n",
       " 'graph_4_12/2018-04-12 17:28:37.568314356~2018-04-12 17:44:07.048741132.txt',\n",
       " 'graph_4_12/2018-04-12 17:44:07.048741132~2018-04-12 17:59:37.072167889.txt',\n",
       " 'graph_4_12/2018-04-12 17:59:37.072167889~2018-04-12 18:14:42.441209885.txt',\n",
       " 'graph_4_12/2018-04-12 18:14:42.441209885~2018-04-12 18:29:50.813709836.txt',\n",
       " 'graph_4_12/2018-04-12 18:29:50.813709836~2018-04-12 18:44:51.996770103.txt',\n",
       " 'graph_4_12/2018-04-12 18:44:51.996770103~2018-04-12 18:59:54.090374716.txt',\n",
       " 'graph_4_12/2018-04-12 18:59:54.090374716~2018-04-12 19:14:54.959663182.txt',\n",
       " 'graph_4_12/2018-04-12 19:14:54.959663182~2018-04-12 19:29:55.160238546.txt',\n",
       " 'graph_4_12/2018-04-12 19:29:55.160238546~2018-04-12 19:44:55.907534312.txt',\n",
       " 'graph_4_12/2018-04-12 19:44:55.907534312~2018-04-12 19:59:56.089291407.txt',\n",
       " 'graph_4_12/2018-04-12 19:59:56.089291407~2018-04-12 20:14:56.869858929.txt',\n",
       " 'graph_4_12/2018-04-12 20:14:56.869858929~2018-04-12 20:30:33.002217462.txt',\n",
       " 'graph_4_12/2018-04-12 20:30:33.002217462~2018-04-12 20:45:41.109871080.txt',\n",
       " 'graph_4_12/2018-04-12 20:45:41.109871080~2018-04-12 21:01:57.550779236.txt',\n",
       " 'graph_4_12/2018-04-12 21:01:57.550779236~2018-04-12 21:17:05.931449051.txt',\n",
       " 'graph_4_12/2018-04-12 21:17:05.931449051~2018-04-12 21:32:59.560160630.txt',\n",
       " 'graph_4_12/2018-04-12 21:32:59.560160630~2018-04-12 21:48:00.113751799.txt',\n",
       " 'graph_4_12/2018-04-12 21:48:00.113751799~2018-04-12 22:03:13.001509745.txt',\n",
       " 'graph_4_12/2018-04-12 22:03:13.001509745~2018-04-12 22:19:00.880054693.txt',\n",
       " 'graph_4_12/2018-04-12 22:19:00.880054693~2018-04-12 22:34:01.001791765.txt',\n",
       " 'graph_4_12/2018-04-12 22:34:01.001791765~2018-04-12 22:49:06.001717261.txt',\n",
       " 'graph_4_12/2018-04-12 22:49:06.001717261~2018-04-12 23:04:45.002044287.txt',\n",
       " 'graph_4_12/2018-04-12 23:04:45.002044287~2018-04-12 23:19:53.001473944.txt',\n",
       " 'graph_4_12/2018-04-12 23:19:53.001473944~2018-04-12 23:34:55.001471636.txt',\n",
       " 'graph_4_12/2018-04-12 23:34:55.001471636~2018-04-12 23:51:06.307836193.txt']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "attack_list=[\n",
    "    \n",
    "'graph_4_10/2018-04-10 13:31:14.548738409~2018-04-10 13:46:36.161065223.txt',\n",
    "'graph_4_10/2018-04-10 14:02:17.001271389~2018-04-10 14:17:34.001373488.txt',\n",
    "'graph_4_10/2018-04-10 14:17:34.001373488~2018-04-10 14:33:18.350772859.txt',\n",
    "'graph_4_10/2018-04-10 14:33:18.350772859~2018-04-10 14:48:47.320442910.txt',\n",
    "'graph_4_10/2018-04-10 14:48:47.320442910~2018-04-10 15:03:54.307022037.txt', \n",
    " \n",
    "'graph_4_12/2018-04-12 12:39:06.592684498~2018-04-12 12:54:44.001888457.txt',\n",
    "'graph_4_12/2018-04-12 12:54:44.001888457~2018-04-12 13:09:55.026832462.txt',\n",
    "'graph_4_12/2018-04-12 13:09:55.026832462~2018-04-12 13:25:06.588370709.txt',\n",
    "'graph_4_12/2018-04-12 13:25:06.588370709~2018-04-12 13:40:07.178206094.txt',\n",
    "]\n",
    "\n",
    "for i in attack_list:\n",
    "    labels[i]=1\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Anomaly Detection"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index_count: 0\n",
      "thr: 4.896203397786591\n",
      "graph_4_9/2018-04-09 08:46:55.004764124~2018-04-09 09:03:31.001287346.txt    0.0  count: 0  percentage: 0.0  node count: 0  edge count: 0\n",
      "index_count: 1\n",
      "thr: 0.8357957622526314\n",
      "graph_4_9/2018-04-09 09:03:31.001287346~2018-04-09 09:20:23.001295997.txt    3.555545348799231  count: 89  percentage: 0.014485677083333334  node count: 25  edge count: 25\n",
      "index_count: 2\n",
      "thr: 1.224309050597831\n",
      "graph_4_9/2018-04-09 09:20:23.001295997~2018-04-09 09:36:02.001305059.txt    4.106366035937852  count: 100  percentage: 0.0244140625  node count: 13  edge count: 15\n",
      "index_count: 3\n",
      "thr: 1.8388852985190178\n",
      "graph_4_9/2018-04-09 09:36:02.001305059~2018-04-09 09:51:31.358485271.txt    3.4349075346595885  count: 1265  percentage: 0.04575376157407408  node count: 91  edge count: 108\n",
      "index_count: 4\n",
      "thr: 1.733747956210153\n",
      "graph_4_9/2018-04-09 09:51:31.358485271~2018-04-09 10:06:48.907525397.txt    3.0829093113758903  count: 2673  percentage: 0.0580078125  node count: 77  edge count: 84\n",
      "index_count: 5\n",
      "thr: 1.6978707679820886\n",
      "graph_4_9/2018-04-09 10:06:48.907525397~2018-04-09 10:23:05.001376520.txt    3.3818198032953655  count: 3502  percentage: 0.04621516047297297  node count: 129  edge count: 141\n",
      "index_count: 6\n",
      "thr: 1.9950313254460936\n",
      "graph_4_9/2018-04-09 10:23:05.001376520~2018-04-09 10:39:33.001366978.txt    3.8951836974427025  count: 1343  percentage: 0.06902754934210527  node count: 71  edge count: 73\n",
      "index_count: 7\n",
      "thr: 1.6590607089000837\n",
      "graph_4_9/2018-04-09 10:39:33.001366978~2018-04-09 10:58:03.657948688.txt    4.100716758603127  count: 489  percentage: 0.04775390625  node count: 61  edge count: 63\n",
      "index_count: 8\n",
      "thr: 1.8301424679955378\n",
      "graph_4_9/2018-04-09 10:58:03.657948688~2018-04-09 11:13:16.001349955.txt    3.674551987302461  count: 2415  percentage: 0.060471754807692304  node count: 82  edge count: 92\n",
      "index_count: 9\n",
      "thr: 1.755807557958652\n",
      "graph_4_9/2018-04-09 11:13:16.001349955~2018-04-09 11:29:58.243560095.txt    2.394344044393257  count: 2847  percentage: 0.13239397321428573  node count: 58  edge count: 58\n",
      "index_count: 10\n",
      "thr: 1.9086542177350145\n",
      "graph_4_9/2018-04-09 11:29:58.243560095~2018-04-09 11:48:04.809192587.txt    3.9378229419326765  count: 631  percentage: 0.04740084134615385  node count: 68  edge count: 68\n",
      "index_count: 11\n",
      "thr: 1.468768567240501\n",
      "graph_4_9/2018-04-09 11:48:04.809192587~2018-04-09 12:03:32.001348097.txt    3.172818316848751  count: 321  percentage: 0.05224609375  node count: 29  edge count: 27\n",
      "index_count: 12\n",
      "thr: 0.9947709104674847\n",
      "graph_4_9/2018-04-09 12:03:32.001348097~2018-04-09 12:20:47.001308311.txt    4.349306253286204  count: 143  percentage: 0.01396484375  node count: 41  edge count: 40\n",
      "index_count: 13\n",
      "thr: 1.3018696852894296\n",
      "graph_4_9/2018-04-09 12:20:47.001308311~2018-04-09 12:39:09.419323601.txt    4.220506310462521  count: 98  percentage: 0.02392578125  node count: 15  edge count: 14\n",
      "index_count: 14\n",
      "thr: 1.4800044042297131\n",
      "graph_4_9/2018-04-09 12:39:09.419323601~2018-04-09 12:57:31.001362714.txt    4.5984277682257275  count: 111  percentage: 0.027099609375  node count: 15  edge count: 14\n",
      "index_count: 15\n",
      "thr: 0.9693555648235787\n",
      "graph_4_9/2018-04-09 12:57:31.001362714~2018-04-09 13:15:43.001302338.txt    4.149618571659043  count: 159  percentage: 0.014115767045454546  node count: 24  edge count: 25\n",
      "index_count: 16\n",
      "thr: 2.0122797078985584\n",
      "graph_4_9/2018-04-09 13:15:43.001302338~2018-04-09 13:33:39.419905659.txt    3.7387957979768336  count: 3106  percentage: 0.051410222457627115  node count: 104  edge count: 117\n",
      "index_count: 17\n",
      "thr: 1.6007985850506503\n",
      "graph_4_9/2018-04-09 13:33:39.419905659~2018-04-09 13:51:11.470466610.txt    3.3145154363915377  count: 1319  percentage: 0.04954176682692308  node count: 73  edge count: 77\n",
      "index_count: 18\n",
      "thr: 1.27411337577895\n",
      "graph_4_9/2018-04-09 13:51:11.470466610~2018-04-09 14:07:06.153449737.txt    4.495452275220858  count: 87  percentage: 0.021240234375  node count: 13  edge count: 12\n",
      "index_count: 19\n",
      "thr: 2.0388096426471853\n",
      "graph_4_9/2018-04-09 14:07:06.153449737~2018-04-09 14:22:13.001279667.txt    3.834774838678913  count: 1690  percentage: 0.0634765625  node count: 77  edge count: 84\n",
      "index_count: 20\n",
      "thr: 2.1100136650779335\n",
      "graph_4_9/2018-04-09 14:22:13.001279667~2018-04-09 14:37:23.941320412.txt    3.3944665557622247  count: 2315  percentage: 0.08074079241071429  node count: 75  edge count: 82\n",
      "index_count: 21\n",
      "thr: 0.894377594865679\n",
      "graph_4_9/2018-04-09 14:37:23.941320412~2018-04-09 14:55:15.001271684.txt    3.7464826154327446  count: 126  percentage: 0.013671875  node count: 14  edge count: 13\n",
      "index_count: 22\n",
      "thr: 0.8373058801905673\n",
      "graph_4_9/2018-04-09 14:55:15.001271684~2018-04-09 15:10:49.001341356.txt    3.649163442798857  count: 117  percentage: 0.0126953125  node count: 13  edge count: 12\n",
      "index_count: 23\n",
      "thr: 1.3263057349012277\n",
      "graph_4_9/2018-04-09 15:10:49.001341356~2018-04-09 15:26:57.001352104.txt    3.975069496210416  count: 85  percentage: 0.027669270833333332  node count: 17  edge count: 14\n",
      "index_count: 24\n",
      "thr: 1.519445537362048\n",
      "graph_4_9/2018-04-09 15:26:57.001352104~2018-04-09 15:45:04.001267664.txt    4.4666801691051665  count: 123  percentage: 0.030029296875  node count: 17  edge count: 16\n",
      "index_count: 25\n",
      "thr: 1.3985905357568529\n",
      "graph_4_9/2018-04-09 15:45:04.001267664~2018-04-09 16:03:49.001367284.txt    4.97298313644142  count: 90  percentage: 0.02197265625  node count: 15  edge count: 14\n",
      "index_count: 26\n",
      "thr: 1.3110502803839275\n",
      "graph_4_9/2018-04-09 16:03:49.001367284~2018-04-09 16:21:55.001365897.txt    3.7183538285732953  count: 153  percentage: 0.0298828125  node count: 19  edge count: 17\n",
      "index_count: 27\n",
      "thr: 0.7683900046555857\n",
      "graph_4_9/2018-04-09 16:21:55.001365897~2018-04-09 16:37:01.656413926.txt    3.35361195281127  count: 160  percentage: 0.01201923076923077  node count: 17  edge count: 17\n",
      "index_count: 28\n",
      "thr: 1.6873904387788792\n",
      "graph_4_9/2018-04-09 16:37:01.656413926~2018-04-09 16:52:03.637955316.txt    2.9963114657709538  count: 2170  percentage: 0.07063802083333333  node count: 78  edge count: 87\n",
      "index_count: 29\n",
      "thr: 1.8143176755371146\n",
      "graph_4_9/2018-04-09 16:52:03.637955316~2018-04-09 17:21:00.121556708.txt    3.1138124791321284  count: 2887  percentage: 0.06876429115853659  node count: 73  edge count: 77\n",
      "index_count: 30\n",
      "thr: 1.9784411849502028\n",
      "graph_4_9/2018-04-09 17:21:00.121556708~2018-04-09 17:36:15.628929147.txt    3.596478050267176  count: 3138  percentage: 0.06520113031914894  node count: 84  edge count: 94\n",
      "index_count: 31\n",
      "thr: 1.7732570269775025\n",
      "graph_4_9/2018-04-09 17:36:15.628929147~2018-04-09 17:53:19.648909478.txt    3.2735846821256733  count: 2344  percentage: 0.06186655405405406  node count: 78  edge count: 87\n",
      "index_count: 32\n",
      "thr: 1.913489407311888\n",
      "graph_4_9/2018-04-09 17:53:19.648909478~2018-04-09 18:12:18.001372515.txt    3.599527403226598  count: 1933  percentage: 0.06292317708333334  node count: 77  edge count: 83\n"
     ]
    }
   ],
   "source": [
    "# node_IDF_3=torch.load(\"node_IDF_4_3\")\n",
    "node_IDF=torch.load(\"node_IDF_4_3-5\")\n",
    "file_list=[]\n",
    "\n",
    "file_path=\"graph_4_9/\"\n",
    "file_l=os.listdir(\"graph_4_9/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "    \n",
    "    \n",
    "    \n",
    "# node_IDF_410=torch.load(\"node_IDF_4_10\")\n",
    "# node_IDF=torch.load(\"node_IDF_4_12\")\n",
    "y_data_4_10=[]\n",
    "df_list_4_10=[]\n",
    "# node_set_list=[]\n",
    "history_list=[]\n",
    "tw_que=[]\n",
    "his_tw={}\n",
    "current_tw={}\n",
    "\n",
    "\n",
    "\n",
    "file_path_list=[]\n",
    "\n",
    "\n",
    "file_path=\"graph_4_9/\"\n",
    "file_l=os.listdir(\"graph_4_9/\")\n",
    "for i in file_l:\n",
    "    file_path_list.append(file_path+i)\n",
    "\n",
    "# file_path=\"graph_4_12/\"\n",
    "# file_l=os.listdir(\"graph_4_12/\")\n",
    "# for i in file_l:\n",
    "#     file_path_list.append(file_path+i)\n",
    "\n",
    "\n",
    "index_count=0\n",
    "for f_path in sorted(file_path_list):\n",
    "    f=open(f_path)\n",
    "    edge_loss_list=[]\n",
    "    edge_list=[]\n",
    "    print('index_count:',index_count)\n",
    "    \n",
    "    for line in f:\n",
    "        l=line.strip()\n",
    "        jdata=eval(l)\n",
    "        edge_loss_list.append(jdata['loss'])\n",
    "        edge_list.append([str(jdata['srcmsg']),str(jdata['dstmsg'])])\n",
    "    df_list_4_10.append(pd.DataFrame(edge_loss_list))\n",
    "    count,loss_avg,node_set,edge_set=cal_anomaly_loss(edge_loss_list,edge_list,\"graph_4_10_without_neg_edge/\")\n",
    "    current_tw={}\n",
    "    current_tw['name']=f_path\n",
    "    current_tw['loss']=loss_avg\n",
    "    current_tw['index']=index_count\n",
    "    current_tw['nodeset']=node_set\n",
    "\n",
    "    added_que_flag=False\n",
    "    for hq in history_list:\n",
    "        for his_tw in hq:\n",
    "            if cal_set_rel_bak(current_tw['nodeset'],his_tw['nodeset'],file_list)!=0 and current_tw['name']!=his_tw['name']:\n",
    "                print(\"history queue:\",his_tw['name'])\n",
    "                hq.append(copy.deepcopy(current_tw))\n",
    "                added_que_flag=True\n",
    "                break\n",
    "        if added_que_flag:\n",
    "            break\n",
    "    if added_que_flag is False:\n",
    "        temp_hq=[copy.deepcopy(current_tw)]\n",
    "        history_list.append(temp_hq)\n",
    "  \n",
    "    index_count+=1\n",
    "#     node_set_list.append(node_set)\n",
    "    print( f_path,\"  \",loss_avg,\" count:\",count,\" percentage:\",count/len(edge_list),\" node count:\",len(node_set),\" edge count:\",len(edge_set))\n",
    "#     y_data_4_10.append([loss_avg,labels_4_10[f_path],f_path])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# pred_label={}\n",
    "\n",
    "# files = os.listdir(\"graph_4_9\")\n",
    "# for f in files:\n",
    "#     pred_label[\"graph_4_9/\"+f]=0\n",
    "\n",
    "# files = os.listdir(\"graph_4_10\")\n",
    "# for f in files:\n",
    "#     pred_label[\"graph_4_10/\"+f]=0\n",
    "\n",
    "# files = os.listdir(\"graph_4_11\")\n",
    "# for f in files:\n",
    "#     pred_label[\"graph_4_11/\"+f]=0\n",
    "\n",
    "# files = os.listdir(\"graph_4_12\")\n",
    "# for f in files:\n",
    "#     pred_label[\"graph_4_12/\"+f]=0\n",
    "\n",
    "\n",
    "for hl in history_list:\n",
    "    loss_count=0\n",
    "    for hq in hl:\n",
    "        if loss_count==0:\n",
    "            loss_count=(loss_count+1)*(hq['loss']+1)\n",
    "        else:\n",
    "            loss_count=(loss_count)*(hq['loss']+1)\n",
    "\n",
    "    if loss_count>10:\n",
    "#     if loss_count>50:\n",
    "        name_list=[]\n",
    "        for i in hl:\n",
    "            name_list.append(i['name'])\n",
    "            print(i['name'])\n",
    "#         print(name_list)\n",
    "        print(loss_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index_count: 0\n",
      "thr: 1.6961196294075331\n",
      "graph_4_10/2018-04-10 12:44:33.449564893~2018-04-10 13:00:02.700560774.txt    2.8816383875021523  count: 295  percentage: 0.0576171875  node count: 50  edge count: 52\n",
      "index_count: 1\n",
      "thr: 0.9227572540733142\n",
      "graph_4_10/2018-04-10 13:00:02.700560774~2018-04-10 13:16:13.551944728.txt    2.3235348745849627  count: 4774  percentage: 0.03984708867521367  node count: 355  edge count: 431\n",
      "index_count: 2\n",
      "thr: 1.6388562578993828\n",
      "node: {'file': '/run/shm/pulse-shm-693976519'}  IDF: 3.7612001156935624\n",
      "history queue: graph_4_10/2018-04-10 13:00:02.700560774~2018-04-10 13:16:13.551944728.txt\n",
      "graph_4_10/2018-04-10 13:16:13.551944728~2018-04-10 13:31:14.548738409.txt    3.0993020363765034  count: 7924  percentage: 0.06728940217391305  node count: 1026  edge count: 1813\n",
      "index_count: 3\n",
      "thr: 1.5368102613180425\n",
      "graph_4_10/2018-04-10 13:31:14.548738409~2018-04-10 13:46:36.161065223.txt    2.6232939231734034  count: 17723  percentage: 0.07761263312780269  node count: 886  edge count: 1245\n",
      "index_count: 4\n",
      "thr: 0.9813145192481383\n",
      "graph_4_10/2018-04-10 13:46:36.161065223~2018-04-10 14:02:17.001271389.txt    2.246101986648626  count: 351  percentage: 0.0428466796875  node count: 29  edge count: 29\n",
      "index_count: 5\n",
      "thr: 1.4197908202721923\n",
      "node: {'file': '/home/admin/.gconf/desktop/gnome/url-handlers/https/%gconf.xml.new'}  IDF: 3.7612001156935624\n",
      "node: {'file': '/home/admin/.gconf/desktop/gnome/url-handlers/ftp/%gconf.xml.new'}  IDF: 3.7612001156935624\n",
      "node: {'file': '/home/admin/.gconf/desktop/gnome/url-handlers/http/%gconf.xml.new'}  IDF: 3.7612001156935624\n",
      "node: {'file': '/home/admin/.gconf/desktop/gnome/url-handlers/chrome/%gconf.xml.new'}  IDF: 3.7612001156935624\n",
      "history queue: graph_4_10/2018-04-10 13:31:14.548738409~2018-04-10 13:46:36.161065223.txt\n",
      "graph_4_10/2018-04-10 14:02:17.001271389~2018-04-10 14:17:34.001373488.txt    2.8894892006084647  count: 1508  percentage: 0.05078125  node count: 167  edge count: 189\n",
      "index_count: 6\n",
      "thr: 0.5038312781137114\n",
      "node: {'file': '/home/admin/.mozilla/firefox/Crash Reports/LastCrash'}  IDF: 3.7612001156935624\n",
      "node: {'file': '/home/admin/.mozilla/firefox/Crash Reports/crashreporter.ini'}  IDF: 3.7612001156935624\n",
      "node: {'file': '/home/admin/Downloads/firefox/crashreporter'}  IDF: 3.7612001156935624\n",
      "node: {'file': '/lib/x86_64-linux-gnu/ld-2.15.so'}  IDF: 4.169245870395218\n",
      "node: {'subject': '/home/admin/Downloads/firefox/crashreporter'}  IDF: 3.7612001156935624\n",
      "node: {'file': '/home/admin/Downloads/firefox/minidump-analyzer'}  IDF: 4.574710978503383\n",
      "history queue: graph_4_10/2018-04-10 13:31:14.548738409~2018-04-10 13:46:36.161065223.txt\n",
      "graph_4_10/2018-04-10 14:17:34.001373488~2018-04-10 14:33:18.350772859.txt    0.9377001688645288  count: 143635  percentage: 0.05678888853744939  node count: 753  edge count: 1284\n",
      "index_count: 7\n",
      "thr: 1.581248270066185\n",
      "node: {'file': '/home/admin'}  IDF: 4.169245870395218\n",
      "node: {'file': '/home/admin/.bash_logout'}  IDF: 4.169245870395218\n",
      "history queue: graph_4_10/2018-04-10 14:17:34.001373488~2018-04-10 14:33:18.350772859.txt\n",
      "graph_4_10/2018-04-10 14:33:18.350772859~2018-04-10 14:48:47.320442910.txt    2.610405856924556  count: 7364  percentage: 0.08664344879518072  node count: 170  edge count: 202\n",
      "index_count: 8\n",
      "thr: 1.4526068350622232\n",
      "graph_4_10/2018-04-10 14:48:47.320442910~2018-04-10 15:03:54.307022037.txt    2.8023660019286405  count: 7138  percentage: 0.05321147423664122  node count: 690  edge count: 928\n",
      "index_count: 9\n",
      "thr: 1.5684864830272054\n",
      "node: {'subject': '/home/admin/profile'}  IDF: 3.7612001156935624\n",
      "history queue: graph_4_10/2018-04-10 14:48:47.320442910~2018-04-10 15:03:54.307022037.txt\n",
      "graph_4_10/2018-04-10 15:03:54.307022037~2018-04-10 15:19:25.001773315.txt    2.881476187756901  count: 10303  percentage: 0.06097892992424243  node count: 711  edge count: 806\n",
      "index_count: 10\n",
      "thr: 1.492594120044667\n",
      "graph_4_10/2018-04-10 15:19:25.001773315~2018-04-10 15:36:13.002273705.txt    3.310545681596057  count: 1273  percentage: 0.051798502604166664  node count: 241  edge count: 379\n",
      "index_count: 11\n",
      "thr: 1.2174209590304894\n",
      "graph_4_10/2018-04-10 15:36:13.002273705~2018-04-10 15:51:24.614585595.txt    2.614842819209053  count: 1194  percentage: 0.05300071022727273  node count: 168  edge count: 214\n",
      "index_count: 12\n",
      "thr: 1.1683968878378037\n",
      "graph_4_10/2018-04-10 15:51:24.614585595~2018-04-10 16:06:34.924760399.txt    2.3820296324068426  count: 3224  percentage: 0.05940448113207547  node count: 256  edge count: 288\n",
      "index_count: 13\n",
      "thr: 1.3042604977885146\n",
      "graph_4_10/2018-04-10 16:06:34.924760399~2018-04-10 16:21:58.949085533.txt    2.4199871047541133  count: 9147  percentage: 0.06246585445804196  node count: 703  edge count: 745\n",
      "index_count: 14\n",
      "thr: 1.112634424206153\n",
      "graph_4_10/2018-04-10 16:21:58.949085533~2018-04-10 16:37:32.001491870.txt    2.0924914251049858  count: 11871  percentage: 0.06300420346467392  node count: 686  edge count: 751\n",
      "index_count: 15\n",
      "thr: 1.6137418190707304\n",
      "graph_4_10/2018-04-10 16:37:32.001491870~2018-04-10 16:52:38.298464794.txt    2.8759922883972826  count: 10991  percentage: 0.06750565055031446  node count: 715  edge count: 1067\n",
      "index_count: 16\n",
      "thr: 1.4808011630585947\n",
      "graph_4_10/2018-04-10 16:52:38.298464794~2018-04-10 17:07:45.640852929.txt    2.860272297331359  count: 13325  percentage: 0.056576936141304345  node count: 1081  edge count: 1230\n",
      "index_count: 17\n",
      "thr: 1.134342118024868\n",
      "graph_4_10/2018-04-10 17:07:45.640852929~2018-04-10 17:22:45.926569237.txt    2.469334762230158  count: 8859  percentage: 0.05000790281791907  node count: 631  edge count: 692\n",
      "index_count: 18\n",
      "thr: 1.2725570444905994\n",
      "graph_4_10/2018-04-10 17:22:45.926569237~2018-04-10 17:37:49.534629845.txt    2.495752236834978  count: 6747  percentage: 0.06215912441037736  node count: 181  edge count: 231\n",
      "index_count: 19\n",
      "thr: 1.2170348208787343\n",
      "graph_4_10/2018-04-10 17:37:49.534629845~2018-04-10 17:52:50.724731198.txt    2.2270671440353507  count: 12418  percentage: 0.06699974102209945  node count: 820  edge count: 903\n",
      "index_count: 20\n",
      "thr: 1.4791886163287085\n",
      "graph_4_10/2018-04-10 17:52:50.724731198~2018-04-10 18:08:02.001452859.txt    2.8070201042864023  count: 9806  percentage: 0.056663738905325445  node count: 536  edge count: 568\n",
      "index_count: 21\n",
      "thr: 1.5696548596469528\n",
      "graph_4_10/2018-04-10 18:08:02.001452859~2018-04-10 18:23:10.812709243.txt    2.8587507929669362  count: 11547  percentage: 0.05998067652925532  node count: 815  edge count: 866\n",
      "index_count: 22\n",
      "thr: 1.4956974619885581\n",
      "graph_4_10/2018-04-10 18:23:10.812709243~2018-04-10 18:38:10.882714584.txt    2.88824777202994  count: 12387  percentage: 0.05328933783039647  node count: 909  edge count: 1176\n",
      "index_count: 23\n",
      "thr: 1.065175655083452\n",
      "graph_4_10/2018-04-10 18:38:10.882714584~2018-04-10 18:54:51.062762969.txt    2.1418306488563417  count: 8324  percentage: 0.05684549825174825  node count: 535  edge count: 635\n",
      "index_count: 24\n",
      "thr: 1.6697683470680547\n",
      "graph_4_10/2018-04-10 18:54:51.062762969~2018-04-10 19:10:03.757528839.txt    2.7066886062316415  count: 8082  percentage: 0.0724089736238532  node count: 457  edge count: 582\n",
      "index_count: 25\n",
      "thr: 1.4659181074584717\n",
      "graph_4_10/2018-04-10 19:10:03.757528839~2018-04-10 19:26:29.001922190.txt    2.6586515129542865  count: 2178  percentage: 0.07596261160714286  node count: 420  edge count: 672\n",
      "index_count: 26\n",
      "thr: 1.0578501744926057\n",
      "graph_4_10/2018-04-10 19:26:29.001922190~2018-04-10 19:41:52.912938380.txt    2.035925666815556  count: 1470  percentage: 0.059814453125  node count: 235  edge count: 247\n",
      "index_count: 27\n",
      "thr: 1.394112042500725\n",
      "graph_4_10/2018-04-10 19:41:52.912938380~2018-04-10 19:57:35.001557519.txt    2.6621996250833706  count: 3500  percentage: 0.05996436403508772  node count: 360  edge count: 528\n",
      "index_count: 28\n",
      "thr: 1.1142638147516504\n",
      "graph_4_10/2018-04-10 19:57:35.001557519~2018-04-10 20:13:24.002153177.txt    1.9729267643263593  count: 1915  percentage: 0.06926359953703703  node count: 252  edge count: 275\n",
      "index_count: 29\n",
      "thr: 1.2327417393422841\n",
      "graph_4_10/2018-04-10 20:13:24.002153177~2018-04-10 20:29:32.050293487.txt    2.297590073777831  count: 3714  percentage: 0.06253367456896551  node count: 329  edge count: 352\n",
      "index_count: 30\n",
      "thr: 1.2000978610841313\n",
      "graph_4_10/2018-04-10 20:29:32.050293487~2018-04-10 20:44:46.874595566.txt    2.247138564103127  count: 3575  percentage: 0.062343052455357144  node count: 376  edge count: 397\n",
      "index_count: 31\n",
      "thr: 0.7849592136198801\n",
      "graph_4_10/2018-04-10 20:44:46.874595566~2018-04-10 21:00:56.078860802.txt    1.7768601621641873  count: 462  percentage: 0.050130208333333336  node count: 61  edge count: 64\n",
      "index_count: 32\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "thr: 1.193937106516736\n",
      "graph_4_10/2018-04-10 21:00:56.078860802~2018-04-10 21:16:35.002073797.txt    2.3201980250529948  count: 3321  percentage: 0.06005859375  node count: 395  edge count: 419\n",
      "index_count: 33\n",
      "thr: 1.0450795828864683\n",
      "graph_4_10/2018-04-10 21:16:35.002073797~2018-04-10 21:32:59.002389345.txt    2.1440501561531495  count: 1365  percentage: 0.0555419921875  node count: 244  edge count: 256\n",
      "index_count: 34\n",
      "thr: 1.0820500450631907\n",
      "graph_4_10/2018-04-10 21:32:59.002389345~2018-04-10 21:48:01.438127726.txt    2.3574418651809306  count: 1009  percentage: 0.049267578125  node count: 144  edge count: 157\n",
      "index_count: 35\n",
      "thr: 1.1449390003687996\n",
      "graph_4_10/2018-04-10 21:48:01.438127726~2018-04-10 22:03:05.222735201.txt    2.2900234066757754  count: 3984  percentage: 0.05329623287671233  node count: 571  edge count: 587\n",
      "index_count: 36\n",
      "thr: 1.1810826602718214\n",
      "graph_4_10/2018-04-10 22:03:05.222735201~2018-04-10 22:18:56.001694554.txt    2.2195560492110995  count: 4543  percentage: 0.05995301942567568  node count: 488  edge count: 572\n",
      "index_count: 37\n",
      "thr: 1.174036324646173\n",
      "graph_4_10/2018-04-10 22:18:56.001694554~2018-04-10 22:35:47.001550655.txt    2.262168380443744  count: 4376  percentage: 0.05935329861111111  node count: 553  edge count: 662\n",
      "index_count: 38\n",
      "thr: 1.2059973956055055\n",
      "graph_4_10/2018-04-10 22:35:47.001550655~2018-04-10 22:51:30.001246535.txt    2.3826390612011386  count: 3348  percentage: 0.055415783898305086  node count: 489  edge count: 519\n",
      "index_count: 39\n",
      "thr: 0.7835325979278742\n",
      "graph_4_10/2018-04-10 22:51:30.001246535~2018-04-10 23:07:01.106168658.txt    1.9610136819447863  count: 339  percentage: 0.0413818359375  node count: 43  edge count: 45\n",
      "index_count: 40\n",
      "thr: 0.8274922539874694\n",
      "graph_4_10/2018-04-10 23:07:01.106168658~2018-04-10 23:22:23.515914390.txt    1.8975426543379963  count: 422  percentage: 0.051513671875  node count: 35  edge count: 35\n",
      "index_count: 41\n",
      "thr: 0.7918376379106389\n",
      "graph_4_10/2018-04-10 23:22:23.515914390~2018-04-10 23:38:02.289791440.txt    1.8435367615134173  count: 405  percentage: 0.0494384765625  node count: 34  edge count: 35\n",
      "index_count: 42\n",
      "thr: 0.7241742327945884\n",
      "graph_4_10/2018-04-10 23:38:02.289791440~2018-04-10 23:53:53.001127888.txt    1.761902639632456  count: 378  percentage: 0.046142578125  node count: 30  edge count: 30\n"
     ]
    }
   ],
   "source": [
    "# node_IDF_3=torch.load(\"node_IDF_4_3\")\n",
    "node_IDF=torch.load(\"node_IDF_4_3-5\")\n",
    "file_list=[]\n",
    "\n",
    "file_path=\"graph_4_10/\"\n",
    "file_l=os.listdir(\"graph_4_10/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "    \n",
    "    \n",
    "    \n",
    "# node_IDF_410=torch.load(\"node_IDF_4_10\")\n",
    "# node_IDF=torch.load(\"node_IDF_4_12\")\n",
    "y_data_4_10=[]\n",
    "df_list_4_10=[]\n",
    "# node_set_list=[]\n",
    "history_list=[]\n",
    "tw_que=[]\n",
    "his_tw={}\n",
    "current_tw={}\n",
    "\n",
    "\n",
    "\n",
    "file_path_list=[]\n",
    "\n",
    "\n",
    "file_path=\"graph_4_10/\"\n",
    "file_l=os.listdir(\"graph_4_10/\")\n",
    "for i in file_l:\n",
    "    file_path_list.append(file_path+i)\n",
    "\n",
    "# file_path=\"graph_4_12/\"\n",
    "# file_l=os.listdir(\"graph_4_12/\")\n",
    "# for i in file_l:\n",
    "#     file_path_list.append(file_path+i)\n",
    "\n",
    "\n",
    "index_count=0\n",
    "for f_path in sorted(file_path_list):\n",
    "    f=open(f_path)\n",
    "    edge_loss_list=[]\n",
    "    edge_list=[]\n",
    "    print('index_count:',index_count)\n",
    "    \n",
    "    for line in f:\n",
    "        l=line.strip()\n",
    "        jdata=eval(l)\n",
    "        edge_loss_list.append(jdata['loss'])\n",
    "        edge_list.append([str(jdata['srcmsg']),str(jdata['dstmsg'])])\n",
    "    df_list_4_10.append(pd.DataFrame(edge_loss_list))\n",
    "    count,loss_avg,node_set,edge_set=cal_anomaly_loss(edge_loss_list,edge_list,\"graph_4_10_without_neg_edge/\")\n",
    "    current_tw={}\n",
    "    current_tw['name']=f_path\n",
    "    current_tw['loss']=loss_avg\n",
    "    current_tw['index']=index_count\n",
    "    current_tw['nodeset']=node_set\n",
    "\n",
    "    added_que_flag=False\n",
    "    for hq in history_list:\n",
    "        for his_tw in hq:\n",
    "            if cal_set_rel_bak(current_tw['nodeset'],his_tw['nodeset'],file_list)!=0 and current_tw['name']!=his_tw['name']:\n",
    "                print(\"history queue:\",his_tw['name'])\n",
    "                hq.append(copy.deepcopy(current_tw))\n",
    "                added_que_flag=True\n",
    "                break\n",
    "        if added_que_flag:\n",
    "            break\n",
    "    if added_que_flag is False:\n",
    "        temp_hq=[copy.deepcopy(current_tw)]\n",
    "        history_list.append(temp_hq)\n",
    "  \n",
    "    index_count+=1\n",
    "#     node_set_list.append(node_set)\n",
    "    print( f_path,\"  \",loss_avg,\" count:\",count,\" percentage:\",count/len(edge_list),\" node count:\",len(node_set),\" edge count:\",len(edge_set))\n",
    "#     y_data_4_10.append([loss_avg,labels_4_10[f_path],f_path])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "graph_4_10/2018-04-10 13:31:14.548738409~2018-04-10 13:46:36.161065223.txt\n",
      "graph_4_10/2018-04-10 14:02:17.001271389~2018-04-10 14:17:34.001373488.txt\n",
      "graph_4_10/2018-04-10 14:17:34.001373488~2018-04-10 14:33:18.350772859.txt\n",
      "graph_4_10/2018-04-10 14:33:18.350772859~2018-04-10 14:48:47.320442910.txt\n",
      "98.59133282732788\n",
      "graph_4_10/2018-04-10 14:48:47.320442910~2018-04-10 15:03:54.307022037.txt\n",
      "graph_4_10/2018-04-10 15:03:54.307022037~2018-04-10 15:19:25.001773315.txt\n",
      "14.758793093622428\n"
     ]
    }
   ],
   "source": [
    "\n",
    "pred_label={}\n",
    "\n",
    "# files = os.listdir(\"graph_4_9\")\n",
    "# for f in files:\n",
    "#     pred_label[\"graph_4_9/\"+f]=0\n",
    "\n",
    "files = os.listdir(\"graph_4_10\")\n",
    "for f in files:\n",
    "    pred_label[\"graph_4_10/\"+f]=0\n",
    "\n",
    "files = os.listdir(\"graph_4_11\")\n",
    "for f in files:\n",
    "    pred_label[\"graph_4_11/\"+f]=0\n",
    "\n",
    "files = os.listdir(\"graph_4_12\")\n",
    "for f in files:\n",
    "    pred_label[\"graph_4_12/\"+f]=0\n",
    "\n",
    "\n",
    "for hl in history_list:\n",
    "    loss_count=0\n",
    "    for hq in hl:\n",
    "        if loss_count==0:\n",
    "            loss_count=(loss_count+1)*(hq['loss']+1)\n",
    "        else:\n",
    "            loss_count=(loss_count)*(hq['loss']+1)\n",
    "\n",
    "    if loss_count>14:\n",
    "#     if loss_count>50:\n",
    "        name_list=[]\n",
    "        for i in hl:\n",
    "            name_list.append(i['name'])\n",
    "            print(i['name'])\n",
    "#         print(name_list)\n",
    "        for i in name_list:\n",
    "            pred_label[i]=1\n",
    "        print(loss_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index_count: 0\n",
      "thr: 2.1253606626891135\n",
      "graph_4_11/2018-04-11 00:00:00.001151329~2018-04-11 00:16:06.001274623.txt    3.7317073737581183  count: 960  percentage: 0.09375  node count: 37  edge count: 41\n",
      "index_count: 1\n",
      "thr: 0.931234243038732\n",
      "graph_4_11/2018-04-11 00:16:06.001274623~2018-04-11 00:32:08.001169192.txt    1.8497416735901286  count: 593  percentage: 0.05791015625  node count: 89  edge count: 92\n",
      "index_count: 2\n",
      "thr: 1.0625132837139786\n",
      "graph_4_11/2018-04-11 00:32:08.001169192~2018-04-11 00:48:31.001594545.txt    2.3259750570480415  count: 384  percentage: 0.0375  node count: 33  edge count: 38\n",
      "index_count: 3\n",
      "thr: 1.0100675618197599\n",
      "graph_4_11/2018-04-11 00:48:31.001594545~2018-04-11 01:04:53.001888013.txt    2.240836043759092  count: 404  percentage: 0.039453125  node count: 31  edge count: 33\n",
      "index_count: 4\n",
      "thr: 1.4711692215524774\n",
      "graph_4_11/2018-04-11 01:04:53.001888013~2018-04-11 01:20:59.002307553.txt    2.4471735387234412  count: 370  percentage: 0.0361328125  node count: 39  edge count: 42\n",
      "index_count: 5\n",
      "thr: 0.8722175917402277\n",
      "graph_4_11/2018-04-11 01:20:59.002307553~2018-04-11 01:37:06.826246565.txt    1.9384974695051063  count: 507  percentage: 0.04951171875  node count: 40  edge count: 44\n",
      "index_count: 6\n",
      "thr: 0.9476923979304958\n",
      "graph_4_11/2018-04-11 01:37:06.826246565~2018-04-11 01:53:33.001963923.txt    2.0346123685482365  count: 458  percentage: 0.0447265625  node count: 32  edge count: 33\n",
      "index_count: 7\n",
      "thr: 1.137309911818881\n",
      "graph_4_11/2018-04-11 01:53:33.001963923~2018-04-11 02:10:00.001194307.txt    2.2170396491662308  count: 469  percentage: 0.04580078125  node count: 32  edge count: 34\n",
      "index_count: 8\n",
      "thr: 1.8353935044666543\n",
      "graph_4_11/2018-04-11 02:10:00.001194307~2018-04-11 02:26:03.001337306.txt    2.9535819042849423  count: 287  percentage: 0.02802734375  node count: 36  edge count: 35\n",
      "index_count: 9\n",
      "thr: 2.2698785719033\n",
      "graph_4_11/2018-04-11 02:26:03.001337306~2018-04-11 02:42:12.001881387.txt    3.4331102669237  count: 168  percentage: 0.01640625  node count: 16  edge count: 15\n",
      "index_count: 10\n",
      "thr: 2.2500641371871355\n",
      "graph_4_11/2018-04-11 02:42:12.001881387~2018-04-11 02:58:40.001723965.txt    3.3636703491209166  count: 190  percentage: 0.0185546875  node count: 14  edge count: 12\n",
      "index_count: 11\n",
      "thr: 3.0233626851417643\n",
      "graph_4_11/2018-04-11 02:58:40.001723965~2018-04-11 03:15:01.001706699.txt    4.523190597007495  count: 87  percentage: 0.00849609375  node count: 11  edge count: 9\n",
      "index_count: 12\n",
      "thr: 3.416588490659034\n",
      "graph_4_11/2018-04-11 03:15:01.001706699~2018-04-11 03:31:06.001379178.txt    6.068680347465147  count: 82  percentage: 0.0080078125  node count: 11  edge count: 9\n",
      "index_count: 13\n",
      "thr: 3.1839332917834624\n",
      "graph_4_11/2018-04-11 03:31:06.001379178~2018-04-11 03:47:16.001779251.txt    3.36211144741194  count: 847  percentage: 0.08271484375  node count: 12  edge count: 12\n",
      "index_count: 14\n",
      "thr: 2.3728959122352418\n",
      "graph_4_11/2018-04-11 03:47:16.001779251~2018-04-11 04:03:26.001627450.txt    3.946179631144272  count: 172  percentage: 0.016796875  node count: 24  edge count: 23\n",
      "index_count: 15\n",
      "thr: 1.5783850393962604\n",
      "graph_4_11/2018-04-11 04:03:26.001627450~2018-04-11 04:19:33.002034536.txt    2.7991243579493585  count: 335  percentage: 0.03271484375  node count: 36  edge count: 35\n",
      "index_count: 16\n",
      "thr: 1.5749838834407366\n",
      "graph_4_11/2018-04-11 04:19:33.002034536~2018-04-11 04:35:40.001832776.txt    2.8110839962647303  count: 383  percentage: 0.03740234375  node count: 48  edge count: 59\n",
      "index_count: 17\n",
      "thr: 0.9619938226869341\n",
      "graph_4_11/2018-04-11 04:35:40.001832776~2018-04-11 04:52:02.636356915.txt    2.145683444257939  count: 402  percentage: 0.0392578125  node count: 32  edge count: 33\n",
      "index_count: 18\n",
      "thr: 0.8888778602474685\n",
      "graph_4_11/2018-04-11 04:52:02.636356915~2018-04-11 05:08:14.807062185.txt    2.199538603604505  count: 415  percentage: 0.04052734375  node count: 32  edge count: 33\n",
      "index_count: 19\n",
      "thr: 0.9125229733367126\n",
      "graph_4_11/2018-04-11 05:08:14.807062185~2018-04-11 05:24:22.001019938.txt    2.0233435620609077  count: 468  percentage: 0.045703125  node count: 39  edge count: 40\n",
      "index_count: 20\n",
      "thr: 0.7952384296892938\n",
      "graph_4_11/2018-04-11 05:24:22.001019938~2018-04-11 05:40:42.002091762.txt    1.8239247402766485  count: 492  percentage: 0.048046875  node count: 34  edge count: 36\n",
      "index_count: 21\n",
      "thr: 1.091515218795392\n",
      "graph_4_11/2018-04-11 05:40:42.002091762~2018-04-11 05:57:05.001981906.txt    2.2887843864978565  count: 404  percentage: 0.039453125  node count: 31  edge count: 32\n",
      "index_count: 22\n",
      "thr: 1.2478733249784018\n",
      "graph_4_11/2018-04-11 05:57:05.001981906~2018-04-11 06:13:20.001263471.txt    2.371279112331468  count: 382  percentage: 0.0373046875  node count: 33  edge count: 34\n",
      "index_count: 23\n",
      "thr: 1.2221655909663705\n",
      "graph_4_11/2018-04-11 06:13:20.001263471~2018-04-11 06:29:17.894355498.txt    2.304019681995837  count: 394  percentage: 0.0384765625  node count: 36  edge count: 38\n",
      "index_count: 24\n",
      "thr: 1.4866559206894014\n",
      "graph_4_11/2018-04-11 06:29:17.894355498~2018-04-11 06:45:36.001432729.txt    2.4977185711888463  count: 334  percentage: 0.0326171875  node count: 35  edge count: 36\n",
      "index_count: 25\n",
      "thr: 1.5300145571035209\n",
      "graph_4_11/2018-04-11 06:45:36.001432729~2018-04-11 07:02:01.002206348.txt    2.4846253292634435  count: 384  percentage: 0.0375  node count: 30  edge count: 30\n",
      "index_count: 26\n",
      "thr: 1.6820600301088469\n",
      "graph_4_11/2018-04-11 07:02:01.002206348~2018-04-11 07:18:14.001812105.txt    3.067826309176129  count: 2899  percentage: 0.07259114583333333  node count: 533  edge count: 860\n",
      "index_count: 27\n",
      "thr: 1.376474783812744\n",
      "graph_4_11/2018-04-11 07:18:14.001812105~2018-04-11 07:34:30.001240773.txt    2.933217258590036  count: 4710  percentage: 0.05348382994186047  node count: 596  edge count: 992\n",
      "index_count: 28\n",
      "thr: 3.4396744537064716\n",
      "node: {'file': '/sbin/initctl'}  IDF: 4.574710978503383\n",
      "history queue: graph_4_11/2018-04-11 07:18:14.001812105~2018-04-11 07:34:30.001240773.txt\n",
      "graph_4_11/2018-04-11 07:34:30.001240773~2018-04-11 07:49:43.130898948.txt    4.847887344410971  count: 32404  percentage: 0.11941332547169811  node count: 3096  edge count: 5705\n",
      "index_count: 29\n",
      "thr: 1.1226857531360253\n",
      "graph_4_11/2018-04-11 07:49:43.130898948~2018-04-11 08:05:01.864920978.txt    2.1367226403280504  count: 2265  percentage: 0.05978146114864865  node count: 263  edge count: 289\n",
      "index_count: 30\n",
      "thr: 1.1912643925326576\n",
      "graph_4_11/2018-04-11 08:05:01.864920978~2018-04-11 08:20:03.240565286.txt    2.3502724249528235  count: 4672  percentage: 0.055640243902439025  node count: 346  edge count: 404\n",
      "index_count: 31\n",
      "thr: 1.2781818551756536\n",
      "graph_4_11/2018-04-11 08:20:03.240565286~2018-04-11 08:35:12.182436041.txt    2.4497667358938475  count: 6975  percentage: 0.05923063858695652  node count: 536  edge count: 732\n",
      "index_count: 32\n",
      "thr: 1.262738080122924\n",
      "graph_4_11/2018-04-11 08:35:12.182436041~2018-04-11 08:51:45.001713561.txt    2.612632048126041  count: 2991  percentage: 0.05841796875  node count: 310  edge count: 413\n",
      "index_count: 33\n",
      "thr: 1.928734311975929\n",
      "graph_4_11/2018-04-11 08:51:45.001713561~2018-04-11 09:07:41.001516635.txt    2.935145677614971  count: 11083  percentage: 0.08455657958984375  node count: 133  edge count: 162\n",
      "index_count: 34\n",
      "thr: 1.2740475714626003\n",
      "node: {'file': '/run/shm/pulse-shm-1468600995'}  IDF: 4.51085950651685\n",
      "history queue: graph_4_11/2018-04-11 08:35:12.182436041~2018-04-11 08:51:45.001713561.txt\n",
      "graph_4_11/2018-04-11 09:07:41.001516635~2018-04-11 09:23:47.001103293.txt    2.234694629277977  count: 5112  percentage: 0.07563920454545454  node count: 169  edge count: 202\n",
      "index_count: 35\n",
      "thr: 1.4608881719223934\n",
      "graph_4_11/2018-04-11 09:23:47.001103293~2018-04-11 09:38:59.728361285.txt    2.788222325343618  count: 7892  percentage: 0.06317238729508197  node count: 855  edge count: 1445\n",
      "index_count: 36\n",
      "thr: 1.20424561634836\n",
      "graph_4_11/2018-04-11 09:38:59.728361285~2018-04-11 09:54:01.833145793.txt    2.2434924486225425  count: 9044  percentage: 0.061762456293706296  node count: 595  edge count: 666\n",
      "index_count: 37\n",
      "thr: 1.4448621349019588\n",
      "graph_4_11/2018-04-11 09:54:01.833145793~2018-04-11 10:09:03.508820548.txt    2.6525910182656287  count: 10856  percentage: 0.0646436737804878  node count: 710  edge count: 1017\n",
      "index_count: 38\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "thr: 1.3834158255815965\n",
      "graph_4_11/2018-04-11 10:09:03.508820548~2018-04-11 10:24:19.673656595.txt    2.527084989808909  count: 13145  percentage: 0.06976583729619565  node count: 539  edge count: 693\n",
      "index_count: 39\n",
      "thr: 1.3812202422396802\n",
      "node: {'file': '/boot'}  IDF: 4.574710978503383\n",
      "history queue: graph_4_11/2018-04-11 08:51:45.001713561~2018-04-11 09:07:41.001516635.txt\n",
      "graph_4_11/2018-04-11 10:24:19.673656595~2018-04-11 10:39:40.241867510.txt    2.3524664385440293  count: 11239  percentage: 0.06494429548816567  node count: 3070  edge count: 3103\n",
      "index_count: 40\n",
      "thr: 1.627427283483102\n",
      "graph_4_11/2018-04-11 10:39:40.241867510~2018-04-11 10:56:00.001497677.txt    3.0039060904661476  count: 11621  percentage: 0.054824313103864736  node count: 531  edge count: 577\n",
      "index_count: 41\n",
      "thr: 1.1573465792496074\n",
      "graph_4_11/2018-04-11 10:56:00.001497677~2018-04-11 11:11:11.539494920.txt    2.1851485619756486  count: 7484  percentage: 0.06141675420168067  node count: 733  edge count: 781\n",
      "index_count: 42\n",
      "thr: 1.3795938331527742\n",
      "graph_4_11/2018-04-11 11:11:11.539494920~2018-04-11 11:26:42.586327848.txt    2.4314496416348055  count: 11184  percentage: 0.07233029801324503  node count: 511  edge count: 569\n",
      "index_count: 43\n",
      "thr: 1.4891424894468308\n",
      "graph_4_11/2018-04-11 11:26:42.586327848~2018-04-11 11:41:54.164805680.txt    2.816595284374125  count: 13266  percentage: 0.047109375  node count: 1022  edge count: 1074\n",
      "index_count: 44\n",
      "thr: 0.8278788482215568\n",
      "graph_4_11/2018-04-11 11:41:54.164805680~2018-04-11 11:57:06.982513263.txt    1.757206582705122  count: 8511  percentage: 0.050990941334355826  node count: 614  edge count: 660\n",
      "index_count: 45\n",
      "thr: 1.2392031043877387\n",
      "graph_4_11/2018-04-11 11:57:06.982513263~2018-04-11 12:13:41.616470541.txt    2.2224246893053414  count: 5444  percentage: 0.066455078125  node count: 574  edge count: 606\n",
      "index_count: 46\n",
      "thr: 1.1763139073179452\n",
      "graph_4_11/2018-04-11 12:13:41.616470541~2018-04-11 12:29:18.001334304.txt    2.2952571174573486  count: 3098  percentage: 0.059321384803921566  node count: 262  edge count: 277\n",
      "index_count: 47\n",
      "thr: 1.2673670807575306\n",
      "graph_4_11/2018-04-11 12:29:18.001334304~2018-04-11 12:44:47.002346666.txt    2.312621902372108  count: 2982  percentage: 0.06772347383720931  node count: 413  edge count: 578\n",
      "index_count: 48\n",
      "thr: 1.010428468653528\n",
      "graph_4_11/2018-04-11 12:44:47.002346666~2018-04-11 13:01:13.002135659.txt    1.9688671773906987  count: 1674  percentage: 0.05637122844827586  node count: 307  edge count: 325\n",
      "index_count: 49\n",
      "thr: 0.9926165199294015\n",
      "graph_4_11/2018-04-11 13:01:13.002135659~2018-04-11 13:16:50.021059784.txt    2.0418753181835325  count: 8380  percentage: 0.05212480095541401  node count: 656  edge count: 696\n",
      "index_count: 50\n",
      "thr: 1.1643561935074125\n",
      "graph_4_11/2018-04-11 13:16:50.021059784~2018-04-11 13:31:56.533212332.txt    2.1630767314245634  count: 8808  percentage: 0.061881744604316544  node count: 687  edge count: 724\n",
      "index_count: 51\n",
      "thr: 1.571566014426575\n",
      "graph_4_11/2018-04-11 13:31:56.533212332~2018-04-11 13:46:57.298929583.txt    3.0963111788129534  count: 13473  percentage: 0.051596966911764706  node count: 1274  edge count: 1947\n",
      "index_count: 52\n",
      "thr: 1.5844951299720031\n",
      "graph_4_11/2018-04-11 13:46:57.298929583~2018-04-11 14:02:03.185872207.txt    3.024749190221269  count: 11114  percentage: 0.053997590174129355  node count: 698  edge count: 889\n",
      "index_count: 53\n",
      "thr: 1.5986170222699438\n",
      "graph_4_11/2018-04-11 14:02:03.185872207~2018-04-11 14:17:10.448756073.txt    2.888728443184714  count: 12091  percentage: 0.06785986889367816  node count: 526  edge count: 618\n",
      "index_count: 54\n",
      "thr: 1.2859564572766944\n",
      "graph_4_11/2018-04-11 14:17:10.448756073~2018-04-11 14:32:13.526157650.txt    2.309072251470688  count: 10972  percentage: 0.0669677734375  node count: 729  edge count: 779\n",
      "index_count: 55\n",
      "thr: 1.4934078345367354\n",
      "graph_4_11/2018-04-11 14:32:13.526157650~2018-04-11 14:47:23.351053223.txt    2.705061969755964  count: 17117  percentage: 0.07023453912815127  node count: 373  edge count: 444\n",
      "index_count: 56\n",
      "thr: 1.5170234652523213\n",
      "graph_4_11/2018-04-11 14:47:23.351053223~2018-04-11 15:03:33.590152518.txt    2.6497390735178437  count: 6556  percentage: 0.058737098623853214  node count: 291  edge count: 334\n",
      "index_count: 57\n",
      "thr: 1.158759871086264\n",
      "graph_4_11/2018-04-11 15:03:33.590152518~2018-04-11 15:20:05.001754985.txt    2.3801324730827638  count: 4305  percentage: 0.05190248842592592  node count: 453  edge count: 493\n",
      "index_count: 58\n",
      "thr: 1.2391445238720815\n",
      "graph_4_11/2018-04-11 15:20:05.001754985~2018-04-11 15:36:18.002102989.txt    2.3700043427165287  count: 3398  percentage: 0.05925641741071429  node count: 251  edge count: 271\n",
      "index_count: 59\n",
      "thr: 1.2545757945335692\n",
      "graph_4_11/2018-04-11 15:36:18.002102989~2018-04-11 15:52:42.862552619.txt    2.512651714950754  count: 3156  percentage: 0.058151533018867926  node count: 423  edge count: 551\n",
      "index_count: 60\n",
      "thr: 1.6311739727632508\n",
      "graph_4_11/2018-04-11 15:52:42.862552619~2018-04-11 16:08:52.001653616.txt    3.203045297769197  count: 5591  percentage: 0.06824951171875  node count: 156  edge count: 182\n",
      "index_count: 61\n",
      "thr: 1.2044896444854543\n",
      "graph_4_11/2018-04-11 16:08:52.001653616~2018-04-11 16:23:53.622260753.txt    2.264547178485782  count: 6836  percentage: 0.06014217342342342  node count: 473  edge count: 502\n",
      "index_count: 62\n",
      "thr: 1.2295188694315504\n",
      "graph_4_11/2018-04-11 16:23:53.622260753~2018-04-11 16:40:20.001018770.txt    2.3975940670776383  count: 11527  percentage: 0.06395929509943182  node count: 409  edge count: 498\n",
      "index_count: 63\n",
      "thr: 1.4837048838695406\n",
      "node: {'file': '/run/shm/pulse-shm-1586793631'}  IDF: 4.51085950651685\n",
      "history queue: graph_4_11/2018-04-11 16:23:53.622260753~2018-04-11 16:40:20.001018770.txt\n",
      "graph_4_11/2018-04-11 16:40:20.001018770~2018-04-11 16:55:20.537127474.txt    2.4104138631446648  count: 6120  percentage: 0.09194711538461539  node count: 153  edge count: 179\n",
      "index_count: 64\n",
      "thr: 1.252639760535509\n",
      "graph_4_11/2018-04-11 16:55:20.537127474~2018-04-11 17:11:02.002108596.txt    2.1086558723085678  count: 6550  percentage: 0.08528645833333333  node count: 173  edge count: 214\n",
      "index_count: 65\n",
      "thr: 1.3933491269455067\n",
      "graph_4_11/2018-04-11 17:11:02.002108596~2018-04-11 17:26:02.530643144.txt    2.5488810078799973  count: 4989  percentage: 0.06862070862676056  node count: 274  edge count: 321\n",
      "index_count: 66\n",
      "thr: 1.6016877335434327\n",
      "graph_4_11/2018-04-11 17:26:02.530643144~2018-04-11 17:41:17.001646675.txt    3.171761104915556  count: 8993  percentage: 0.05816044081125828  node count: 345  edge count: 403\n",
      "index_count: 67\n",
      "thr: 1.1847103739985025\n",
      "graph_4_11/2018-04-11 17:41:17.001646675~2018-04-11 17:56:19.699660881.txt    2.2444783044250456  count: 8885  percentage: 0.05862674197635135  node count: 610  edge count: 645\n",
      "index_count: 68\n",
      "thr: 1.2878885560353286\n",
      "graph_4_11/2018-04-11 17:56:19.699660881~2018-04-11 18:11:46.356548226.txt    2.3462091585424187  count: 13594  percentage: 0.07061377992021277  node count: 667  edge count: 727\n",
      "index_count: 69\n",
      "thr: 1.2400133718390787\n",
      "graph_4_11/2018-04-11 18:11:46.356548226~2018-04-11 18:26:53.001667593.txt    2.3392191030039076  count: 10368  percentage: 0.06173780487804878  node count: 605  edge count: 665\n",
      "index_count: 70\n",
      "thr: 1.5447250299180668\n",
      "graph_4_11/2018-04-11 18:26:53.001667593~2018-04-11 18:42:17.001965855.txt    2.8665687830837503  count: 15057  percentage: 0.06449167351973684  node count: 601  edge count: 640\n",
      "index_count: 71\n",
      "thr: 0.949548643794527\n",
      "graph_4_11/2018-04-11 18:42:17.001965855~2018-04-11 18:58:43.001951167.txt    2.4106704978692206  count: 1694  percentage: 0.04241786858974359  node count: 106  edge count: 119\n",
      "index_count: 72\n",
      "thr: 0.97785382783216\n",
      "graph_4_11/2018-04-11 18:58:43.001951167~2018-04-11 19:14:49.001363010.txt    2.2380114595287317  count: 4138  percentage: 0.04644845545977012  node count: 491  edge count: 527\n",
      "index_count: 73\n",
      "thr: 1.1636186421097099\n",
      "graph_4_11/2018-04-11 19:14:49.001363010~2018-04-11 19:30:09.001780046.txt    2.265953269054798  count: 4961  percentage: 0.056996783088235296  node count: 483  edge count: 507\n",
      "index_count: 74\n",
      "thr: 0.7112913932612174\n",
      "graph_4_11/2018-04-11 19:30:09.001780046~2018-04-11 19:46:28.001487257.txt    1.8191619269477068  count: 412  percentage: 0.040234375  node count: 37  edge count: 37\n",
      "index_count: 75\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "thr: 1.1521095022059136\n",
      "graph_4_11/2018-04-11 19:46:28.001487257~2018-04-11 20:02:07.594306306.txt    2.306313675554065  count: 4541  percentage: 0.05408012576219512  node count: 383  edge count: 404\n",
      "index_count: 76\n",
      "thr: 1.0530830364127706\n",
      "graph_4_11/2018-04-11 20:02:07.594306306~2018-04-11 20:17:10.001507207.txt    2.1705392598210036  count: 1331  percentage: 0.0519921875  node count: 217  edge count: 228\n",
      "index_count: 77\n",
      "thr: 1.154243082366835\n",
      "graph_4_11/2018-04-11 20:17:10.001507207~2018-04-11 20:33:47.001341878.txt    2.251351818603958  count: 5071  percentage: 0.0556421172752809  node count: 575  edge count: 603\n",
      "index_count: 78\n",
      "thr: 1.0788023244447418\n",
      "graph_4_11/2018-04-11 20:33:47.001341878~2018-04-11 20:48:52.002055784.txt    2.1371006926343425  count: 2154  percentage: 0.05535567434210526  node count: 335  edge count: 346\n",
      "index_count: 79\n",
      "thr: 1.0949974168022907\n",
      "graph_4_11/2018-04-11 20:48:52.002055784~2018-04-11 21:03:56.946916791.txt    2.067525110466119  count: 3511  percentage: 0.06234019886363636  node count: 387  edge count: 409\n",
      "index_count: 80\n",
      "thr: 1.189815708213224\n",
      "graph_4_11/2018-04-11 21:03:56.946916791~2018-04-11 21:20:04.001983239.txt    2.263094538515298  count: 4489  percentage: 0.06088595920138889  node count: 466  edge count: 504\n",
      "index_count: 81\n",
      "thr: 1.162476389765977\n",
      "graph_4_11/2018-04-11 21:20:04.001983239~2018-04-11 21:36:36.001464315.txt    2.281884951542143  count: 3479  percentage: 0.057584083686440676  node count: 499  edge count: 628\n",
      "index_count: 82\n",
      "thr: 1.1534787759521596\n",
      "graph_4_11/2018-04-11 21:36:36.001464315~2018-04-11 21:53:05.001134741.txt    2.1771988627200636  count: 4161  percentage: 0.05889096467391304  node count: 546  edge count: 569\n",
      "index_count: 83\n",
      "thr: 1.1268649426934074\n",
      "graph_4_11/2018-04-11 21:53:05.001134741~2018-04-11 22:08:43.001339416.txt    2.2948772212795037  count: 3864  percentage: 0.0546875  node count: 443  edge count: 464\n",
      "index_count: 84\n",
      "thr: 1.0960623435701273\n",
      "graph_4_11/2018-04-11 22:08:43.001339416~2018-04-11 22:24:17.001832396.txt    2.178876062750643  count: 2193  percentage: 0.05491286057692308  node count: 285  edge count: 299\n",
      "index_count: 85\n",
      "thr: 1.1534005377694099\n",
      "graph_4_11/2018-04-11 22:24:17.001832396~2018-04-11 22:39:54.002070033.txt    2.1192489389900637  count: 3996  percentage: 0.06194196428571429  node count: 432  edge count: 449\n",
      "index_count: 86\n",
      "thr: 0.7761521363161364\n",
      "graph_4_11/2018-04-11 22:39:54.002070033~2018-04-11 22:56:00.892549695.txt    1.9283787338791913  count: 447  percentage: 0.04365234375  node count: 37  edge count: 38\n",
      "index_count: 87\n",
      "thr: 1.2062546085800514\n",
      "graph_4_11/2018-04-11 22:56:00.892549695~2018-04-11 23:11:03.001690405.txt    2.442366878165822  count: 4620  percentage: 0.05307904411764706  node count: 385  edge count: 410\n",
      "index_count: 88\n",
      "thr: 0.8541012134826742\n",
      "graph_4_11/2018-04-11 23:11:03.001690405~2018-04-11 23:26:56.002055798.txt    2.1227801423956594  count: 426  percentage: 0.0416015625  node count: 49  edge count: 52\n",
      "index_count: 89\n",
      "thr: 0.6841108663882053\n",
      "graph_4_11/2018-04-11 23:26:56.002055798~2018-04-11 23:43:02.705859964.txt    1.7588439236804059  count: 414  percentage: 0.0404296875  node count: 40  edge count: 40\n",
      "index_count: 90\n",
      "thr: 0.7140541732825866\n",
      "graph_4_11/2018-04-11 23:43:02.705859964~2018-04-11 23:59:03.355632261.txt    1.870999267654511  count: 418  percentage: 0.0408203125  node count: 35  edge count: 34\n"
     ]
    }
   ],
   "source": [
    "# node_IDF_3=torch.load(\"node_IDF_4_3\")\n",
    "node_IDF=torch.load(\"node_IDF_4_3-5\")\n",
    "file_list=[]\n",
    "\n",
    "file_path=\"graph_4_11/\"\n",
    "file_l=os.listdir(\"graph_4_11/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "    \n",
    "    \n",
    "    \n",
    "# node_IDF_410=torch.load(\"node_IDF_4_10\")\n",
    "# node_IDF=torch.load(\"node_IDF_4_12\")\n",
    "y_data_4_10=[]\n",
    "df_list_4_10=[]\n",
    "# node_set_list=[]\n",
    "history_list=[]\n",
    "tw_que=[]\n",
    "his_tw={}\n",
    "current_tw={}\n",
    "\n",
    "\n",
    "\n",
    "file_path_list=[]\n",
    "\n",
    "\n",
    "file_path=\"graph_4_11/\"\n",
    "file_l=os.listdir(\"graph_4_11/\")\n",
    "for i in file_l:\n",
    "    file_path_list.append(file_path+i)\n",
    "\n",
    "# file_path=\"graph_4_12/\"\n",
    "# file_l=os.listdir(\"graph_4_12/\")\n",
    "# for i in file_l:\n",
    "#     file_path_list.append(file_path+i)\n",
    "\n",
    "\n",
    "index_count=0\n",
    "for f_path in sorted(file_path_list):\n",
    "    f=open(f_path)\n",
    "    edge_loss_list=[]\n",
    "    edge_list=[]\n",
    "    print('index_count:',index_count)\n",
    "    \n",
    "    for line in f:\n",
    "        l=line.strip()\n",
    "        jdata=eval(l)\n",
    "        edge_loss_list.append(jdata['loss'])\n",
    "        edge_list.append([str(jdata['srcmsg']),str(jdata['dstmsg'])])\n",
    "    df_list_4_10.append(pd.DataFrame(edge_loss_list))\n",
    "    count,loss_avg,node_set,edge_set=cal_anomaly_loss(edge_loss_list,edge_list,\"graph_4_11_without_neg_edge/\")\n",
    "    current_tw={}\n",
    "    current_tw['name']=f_path\n",
    "    current_tw['loss']=loss_avg\n",
    "    current_tw['index']=index_count\n",
    "    current_tw['nodeset']=node_set\n",
    "\n",
    "    added_que_flag=False\n",
    "    for hq in history_list:\n",
    "        for his_tw in hq:\n",
    "            if cal_set_rel_bak(current_tw['nodeset'],his_tw['nodeset'],file_list)!=0 and current_tw['name']!=his_tw['name']:\n",
    "                print(\"history queue:\",his_tw['name'])\n",
    "                hq.append(copy.deepcopy(current_tw))\n",
    "                added_que_flag=True\n",
    "                break\n",
    "        if added_que_flag:\n",
    "            break\n",
    "    if added_que_flag is False:\n",
    "        temp_hq=[copy.deepcopy(current_tw)]\n",
    "        history_list.append(temp_hq)\n",
    "  \n",
    "    index_count+=1\n",
    "#     node_set_list.append(node_set)\n",
    "    print( f_path,\"  \",loss_avg,\" count:\",count,\" percentage:\",count/len(edge_list),\" node count:\",len(node_set),\" edge count:\",len(edge_set))\n",
    "#     y_data_4_10.append([loss_avg,labels_4_10[f_path],f_path])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "for hl in history_list:\n",
    "    loss_count=0\n",
    "    for hq in hl:\n",
    "        if loss_count==0:\n",
    "            loss_count=(loss_count+1)*(hq['loss']+1)\n",
    "        else:\n",
    "            loss_count=(loss_count)*(hq['loss']+1)\n",
    "    name_list=[]\n",
    "    if loss_count>25:\n",
    "#     if loss_count>50:\n",
    "        name_list=[]\n",
    "        for i in hl:\n",
    "            name_list.append(i['name'])\n",
    "        print(name_list)\n",
    "#         for i in name_list:\n",
    "#             pred_label[i]=1\n",
    "        print(loss_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4-12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "194"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file_list_3_5=[]\n",
    "\n",
    "file_path=\"graph_4_3/\"\n",
    "file_l=os.listdir(\"graph_4_3/\")\n",
    "for i in file_l:\n",
    "    file_list_3_5.append(file_path+i)\n",
    "\n",
    "file_path=\"graph_4_4/\"\n",
    "file_l=os.listdir(\"graph_4_4/\")\n",
    "for i in file_l:\n",
    "    file_list_3_5.append(file_path+i)\n",
    "\n",
    "file_path=\"graph_4_5/\"\n",
    "file_l=os.listdir(\"graph_4_5/\")\n",
    "for i in file_l:\n",
    "    file_list_3_5.append(file_path+i)\n",
    "    \n",
    "len(file_list_3_5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "93"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(file_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index_count: 0\n",
      "thr: 2.407252328767441\n",
      "graph_4_12/2018-04-12 00:00:00.001773757~2018-04-12 00:15:26.001647081.txt    3.8522625245294364  count: 899  percentage: 0.1097412109375  node count: 22  edge count: 24\n",
      "index_count: 1\n",
      "thr: 0.9207361321961105\n",
      "graph_4_12/2018-04-12 00:15:26.001647081~2018-04-12 00:30:58.002002412.txt    1.8949476497409892  count: 493  percentage: 0.0601806640625  node count: 89  edge count: 90\n",
      "index_count: 2\n",
      "thr: 0.9091707504309587\n",
      "graph_4_12/2018-04-12 00:30:58.002002412~2018-04-12 00:46:27.001835122.txt    1.9861210541521825  count: 470  percentage: 0.057373046875  node count: 34  edge count: 37\n",
      "index_count: 3\n",
      "thr: 0.8500345875075368\n",
      "graph_4_12/2018-04-12 00:46:27.001835122~2018-04-12 01:02:05.698953597.txt    2.161627783693989  count: 352  percentage: 0.04296875  node count: 32  edge count: 32\n",
      "index_count: 4\n",
      "thr: 0.8509562688696666\n",
      "graph_4_12/2018-04-12 01:02:05.698953597~2018-04-12 01:17:27.002277528.txt    2.28994397676901  count: 325  percentage: 0.0396728515625  node count: 37  edge count: 39\n",
      "index_count: 5\n",
      "thr: 0.9667901391649836\n",
      "graph_4_12/2018-04-12 01:17:27.002277528~2018-04-12 01:32:48.001732365.txt    2.1094878914563093  count: 460  percentage: 0.05615234375  node count: 72  edge count: 79\n",
      "index_count: 6\n",
      "thr: 0.867650414156497\n",
      "graph_4_12/2018-04-12 01:32:48.001732365~2018-04-12 01:48:20.001593370.txt    2.021600109367506  count: 414  percentage: 0.050537109375  node count: 34  edge count: 34\n",
      "index_count: 7\n",
      "thr: 0.8312237220740241\n",
      "graph_4_12/2018-04-12 01:48:20.001593370~2018-04-12 02:04:08.001895394.txt    2.0144452292595467  count: 391  percentage: 0.0477294921875  node count: 34  edge count: 33\n",
      "index_count: 8\n",
      "thr: 0.9454524781184039\n",
      "graph_4_12/2018-04-12 02:04:08.001895394~2018-04-12 02:19:33.000943880.txt    2.1115797802253544  count: 459  percentage: 0.0560302734375  node count: 40  edge count: 39\n",
      "index_count: 9\n",
      "thr: 0.8491249207587928\n",
      "graph_4_12/2018-04-12 02:19:33.000943880~2018-04-12 02:35:09.219021781.txt    2.0220338589435802  count: 407  percentage: 0.0496826171875  node count: 38  edge count: 37\n",
      "index_count: 10\n",
      "thr: 0.881685751729985\n",
      "graph_4_12/2018-04-12 02:35:09.219021781~2018-04-12 02:50:55.001941925.txt    2.110730337562793  count: 402  percentage: 0.049072265625  node count: 34  edge count: 34\n",
      "index_count: 11\n",
      "thr: 0.8584057977942132\n",
      "graph_4_12/2018-04-12 02:50:55.001941925~2018-04-12 03:06:27.001769928.txt    1.9105391194917065  count: 442  percentage: 0.053955078125  node count: 35  edge count: 36\n",
      "index_count: 12\n",
      "thr: 0.9302253529886784\n",
      "graph_4_12/2018-04-12 03:06:27.001769928~2018-04-12 03:22:01.001281628.txt    2.044739058129764  count: 468  percentage: 0.05712890625  node count: 40  edge count: 40\n",
      "index_count: 13\n",
      "thr: 0.9008382191485945\n",
      "graph_4_12/2018-04-12 03:22:01.001281628~2018-04-12 03:37:29.838784853.txt    1.9275529224697745  count: 492  percentage: 0.06005859375  node count: 39  edge count: 39\n",
      "index_count: 14\n",
      "thr: 0.9105041739009713\n",
      "graph_4_12/2018-04-12 03:37:29.838784853~2018-04-12 03:53:07.001316649.txt    2.1064549624490576  count: 417  percentage: 0.0509033203125  node count: 34  edge count: 35\n",
      "index_count: 15\n",
      "thr: 0.8358161751247517\n",
      "graph_4_12/2018-04-12 03:53:07.001316649~2018-04-12 04:08:50.001184547.txt    2.053857590378886  count: 370  percentage: 0.045166015625  node count: 34  edge count: 33\n",
      "index_count: 16\n",
      "thr: 1.0413068487839037\n",
      "node: {'file': '/etc/samba/dhcp.conf.new'}  IDF: 2.2300144001592104\n",
      "graph_4_12/2018-04-12 04:08:50.001184547~2018-04-12 04:24:00.001592464.txt    2.502934857639855  count: 397  percentage: 0.0484619140625  node count: 52  edge count: 62\n",
      "index_count: 17\n",
      "thr: 0.8869370044412637\n",
      "graph_4_12/2018-04-12 04:24:00.001592464~2018-04-12 04:39:26.002162640.txt    1.9046094460054401  count: 463  percentage: 0.0565185546875  node count: 41  edge count: 42\n",
      "index_count: 18\n",
      "thr: 0.8052466717000928\n",
      "graph_4_12/2018-04-12 04:39:26.002162640~2018-04-12 04:55:10.001772049.txt    2.0637485038243004  count: 349  percentage: 0.0426025390625  node count: 33  edge count: 31\n",
      "index_count: 19\n",
      "thr: 0.8387888692855017\n",
      "graph_4_12/2018-04-12 04:55:10.001772049~2018-04-12 05:10:51.001908824.txt    2.0893668427942336  count: 371  percentage: 0.0452880859375  node count: 34  edge count: 34\n",
      "index_count: 20\n",
      "thr: 0.8787566337773446\n",
      "graph_4_12/2018-04-12 05:10:51.001908824~2018-04-12 05:26:15.658033392.txt    2.0171837108554196  count: 432  percentage: 0.052734375  node count: 39  edge count: 39\n",
      "index_count: 21\n",
      "thr: 0.847763324914689\n",
      "graph_4_12/2018-04-12 05:26:15.658033392~2018-04-12 05:41:53.000960686.txt    1.9578742849434585  count: 426  percentage: 0.052001953125  node count: 38  edge count: 37\n",
      "index_count: 22\n",
      "thr: 0.8571190666511188\n",
      "graph_4_12/2018-04-12 05:41:53.000960686~2018-04-12 05:57:26.001519319.txt    2.084809407821015  count: 390  percentage: 0.047607421875  node count: 35  edge count: 34\n",
      "index_count: 23\n",
      "thr: 0.9204484430187929\n",
      "graph_4_12/2018-04-12 05:57:26.001519319~2018-04-12 06:13:11.001365623.txt    2.0225571045187962  count: 458  percentage: 0.055908203125  node count: 33  edge count: 33\n",
      "index_count: 24\n",
      "thr: 0.9091164788918524\n",
      "graph_4_12/2018-04-12 06:13:11.001365623~2018-04-12 06:28:17.995464541.txt    2.1610261578308365  count: 399  percentage: 0.0487060546875  node count: 40  edge count: 39\n",
      "index_count: 25\n",
      "thr: 0.8973588900176719\n",
      "graph_4_12/2018-04-12 06:28:17.995464541~2018-04-12 06:44:00.001985599.txt    2.1392126590086202  count: 407  percentage: 0.0496826171875  node count: 38  edge count: 37\n",
      "index_count: 26\n",
      "thr: 0.9557564438835764\n",
      "graph_4_12/2018-04-12 06:44:00.001985599~2018-04-12 06:59:19.188577225.txt    2.187662032579185  count: 439  percentage: 0.0535888671875  node count: 46  edge count: 54\n",
      "index_count: 27\n",
      "thr: 1.6957842084931025\n",
      "graph_4_12/2018-04-12 06:59:19.188577225~2018-04-12 07:14:23.001990475.txt    3.0606132645259776  count: 3132  percentage: 0.07282366071428571  node count: 637  edge count: 1050\n",
      "index_count: 28\n",
      "thr: 1.4973293892991566\n",
      "graph_4_12/2018-04-12 07:14:23.001990475~2018-04-12 07:29:24.250402681.txt    3.1994459084344795  count: 1823  percentage: 0.057428175403225805  node count: 334  edge count: 530\n",
      "index_count: 29\n",
      "thr: 2.113943692808923\n",
      "graph_4_12/2018-04-12 07:29:24.250402681~2018-04-12 07:46:06.002011766.txt    3.771399477807693  count: 6026  percentage: 0.07642552759740259  node count: 1518  edge count: 1732\n",
      "index_count: 30\n",
      "thr: 1.497723289625313\n",
      "graph_4_12/2018-04-12 07:46:06.002011766~2018-04-12 08:02:07.001316523.txt    3.0973007248158906  count: 2280  percentage: 0.05709134615384615  node count: 367  edge count: 584\n",
      "index_count: 31\n",
      "thr: 2.199489083428265\n",
      "graph_4_12/2018-04-12 08:02:07.001316523~2018-04-12 08:17:47.001755201.txt    3.5000525674068337  count: 8489  percentage: 0.06908365885416666  node count: 714  edge count: 1305\n",
      "index_count: 32\n",
      "thr: 1.386359866506\n",
      "graph_4_12/2018-04-12 08:17:47.001755201~2018-04-12 08:33:06.483036511.txt    2.7530337472029762  count: 4920  percentage: 0.05459872159090909  node count: 610  edge count: 881\n",
      "index_count: 33\n",
      "thr: 1.0935853402852698\n",
      "graph_4_12/2018-04-12 08:33:06.483036511~2018-04-12 08:48:09.600264510.txt    2.328645115167619  count: 5585  percentage: 0.04869733537946429  node count: 520  edge count: 603\n",
      "index_count: 34\n",
      "thr: 1.7501608524348007\n",
      "graph_4_12/2018-04-12 08:48:09.600264510~2018-04-12 09:03:50.002384931.txt    2.915064451801623  count: 8167  percentage: 0.06484216209349593  node count: 842  edge count: 866\n",
      "index_count: 35\n",
      "thr: 1.450238565485193\n",
      "graph_4_12/2018-04-12 09:03:50.002384931~2018-04-12 09:19:03.668714727.txt    3.2324275875041  count: 2836  percentage: 0.05225530660377359  node count: 297  edge count: 352\n",
      "index_count: 36\n",
      "thr: 1.4038008863139946\n",
      "graph_4_12/2018-04-12 09:19:03.668714727~2018-04-12 09:34:16.330657912.txt    2.861744150327719  count: 8475  percentage: 0.05517578125  node count: 893  edge count: 1480\n",
      "index_count: 37\n",
      "thr: 1.2588216630581694\n",
      "graph_4_12/2018-04-12 09:34:16.330657912~2018-04-12 09:49:24.841744252.txt    2.382121554375323  count: 12112  percentage: 0.06428328804347826  node count: 754  edge count: 933\n",
      "index_count: 38\n",
      "thr: 1.2271826120110132\n",
      "graph_4_12/2018-04-12 09:49:24.841744252~2018-04-12 10:04:31.967749880.txt    2.248745884900745  count: 9673  percentage: 0.06426046981292517  node count: 700  edge count: 781\n",
      "index_count: 39\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "thr: 1.2065697277799654\n",
      "graph_4_12/2018-04-12 10:04:31.967749880~2018-04-12 10:19:41.864196505.txt    2.33142710129299  count: 9873  percentage: 0.06102279469936709  node count: 671  edge count: 706\n",
      "index_count: 40\n",
      "thr: 1.299596213619609\n",
      "graph_4_12/2018-04-12 10:19:41.864196505~2018-04-12 10:34:55.515804775.txt    2.4214968234300587  count: 8976  percentage: 0.06398266423357664  node count: 523  edge count: 557\n",
      "index_count: 41\n",
      "thr: 1.1758024627263675\n",
      "graph_4_12/2018-04-12 10:34:55.515804775~2018-04-12 10:50:58.001487795.txt    2.2133139857528468  count: 16139  percentage: 0.0685249660326087  node count: 400  edge count: 445\n",
      "index_count: 42\n",
      "thr: 1.184830621525315\n",
      "graph_4_12/2018-04-12 10:50:58.001487795~2018-04-12 11:06:07.082292087.txt    2.270303377486004  count: 6577  percentage: 0.06235778216019418  node count: 496  edge count: 531\n",
      "index_count: 43\n",
      "thr: 1.0386086791954696\n",
      "graph_4_12/2018-04-12 11:06:07.082292087~2018-04-12 11:22:02.614075429.txt    2.1474736665299945  count: 8763  percentage: 0.051551910768072286  node count: 749  edge count: 784\n",
      "index_count: 44\n",
      "thr: 1.0988006139931163\n",
      "graph_4_12/2018-04-12 11:22:02.614075429~2018-04-12 11:38:02.721265247.txt    2.3992319889084275  count: 6070  percentage: 0.05199766995614035  node count: 491  edge count: 522\n",
      "index_count: 45\n",
      "thr: 1.2166812325253784\n",
      "node: {'file': '/run/shm/pulse-shm-60379194'}  IDF: 3.4339872044851463\n",
      "graph_4_12/2018-04-12 11:38:02.721265247~2018-04-12 11:53:05.002139204.txt    2.226662505264303  count: 8165  percentage: 0.060406309185606064  node count: 709  edge count: 748\n",
      "index_count: 46\n",
      "thr: 1.5733423618231335\n",
      "graph_4_12/2018-04-12 11:53:05.002139204~2018-04-12 12:08:50.001259045.txt    3.187558925235183  count: 6149  percentage: 0.05458984375  node count: 523  edge count: 667\n",
      "index_count: 47\n",
      "thr: 1.1832025101296542\n",
      "graph_4_12/2018-04-12 12:08:50.001259045~2018-04-12 12:24:03.925533802.txt    2.5455102927074384  count: 2551  percentage: 0.05084103954081633  node count: 252  edge count: 265\n",
      "index_count: 48\n",
      "thr: 1.133811846133663\n",
      "graph_4_12/2018-04-12 12:24:03.925533802~2018-04-12 12:39:06.592684498.txt    2.0191785754559746  count: 3670  percentage: 0.0689227764423077  node count: 349  edge count: 366\n",
      "index_count: 49\n",
      "thr: 1.5540094833402671\n",
      "node: {'subject': '/home/admin/profile'}  IDF: 2.740840023925201\n",
      "node: {'subject': '/home/admin/profile (deleted)'}  IDF: 2.740840023925201\n",
      "graph_4_12/2018-04-12 12:39:06.592684498~2018-04-12 12:54:44.001888457.txt    3.287502737017623  count: 3276  percentage: 0.04998779296875  node count: 307  edge count: 343\n",
      "index_count: 50\n",
      "thr: 1.348730930590155\n",
      "node: {'subject': '/home/admin/profile'}  IDF: 2.740840023925201\n",
      "node: {'subject': '/home/admin/profile (deleted)'}  IDF: 2.740840023925201\n",
      "graph_4_12/2018-04-12 12:54:44.001888457~2018-04-12 13:09:55.026832462.txt    2.379652491805925  count: 6527  percentage: 0.0741165515988372  node count: 375  edge count: 423\n",
      "index_count: 51\n",
      "thr: 5.435417974761739\n",
      "node: {'subject': '/home/admin/profile'}  IDF: 2.740840023925201\n",
      "graph_4_12/2018-04-12 13:09:55.026832462~2018-04-12 13:25:06.588370709.txt    5.626497033966376  count: 1093  percentage: 0.011118570963541666  node count: 13  edge count: 9\n",
      "index_count: 52\n",
      "thr: 1.6482137609741492\n",
      "node: {'subject': '/home/admin/profile'}  IDF: 2.740840023925201\n",
      "graph_4_12/2018-04-12 13:25:06.588370709~2018-04-12 13:40:07.178206094.txt    3.0795157721062627  count: 7140  percentage: 0.05282315340909091  node count: 402  edge count: 430\n",
      "index_count: 53\n",
      "thr: 1.7155784019891407\n",
      "graph_4_12/2018-04-12 13:40:07.178206094~2018-04-12 13:56:02.001338519.txt    2.777519757336164  count: 9850  percentage: 0.07021270529197081  node count: 380  edge count: 444\n",
      "index_count: 54\n",
      "thr: 0.7533649063644089\n",
      "graph_4_12/2018-04-12 13:56:02.001338519~2018-04-12 14:11:10.272552800.txt    1.72709499527252  count: 8055  percentage: 0.04573378452034884  node count: 169  edge count: 192\n",
      "index_count: 55\n",
      "thr: 1.3230720495653094\n",
      "graph_4_12/2018-04-12 14:11:10.272552800~2018-04-12 14:26:13.739235539.txt    2.661556773197893  count: 8623  percentage: 0.05972268395390071  node count: 395  edge count: 431\n",
      "index_count: 56\n",
      "thr: 1.7009953598685053\n",
      "graph_4_12/2018-04-12 14:26:13.739235539~2018-04-12 14:41:15.795737707.txt    3.0412639131987818  count: 9411  percentage: 0.06472133582746478  node count: 370  edge count: 415\n",
      "index_count: 57\n",
      "thr: 1.249643418997705\n",
      "graph_4_12/2018-04-12 14:41:15.795737707~2018-04-12 14:56:26.556444597.txt    2.069170485133103  count: 6592  percentage: 0.07315340909090909  node count: 415  edge count: 435\n",
      "index_count: 58\n",
      "thr: 1.1969377835828832\n",
      "graph_4_12/2018-04-12 14:56:26.556444597~2018-04-12 15:11:30.532680843.txt    2.001048753940021  count: 6220  percentage: 0.07146139705882353  node count: 442  edge count: 466\n",
      "index_count: 59\n",
      "thr: 1.2841720809338555\n",
      "graph_4_12/2018-04-12 15:11:30.532680843~2018-04-12 15:26:31.776115575.txt    2.2087736911219125  count: 5899  percentage: 0.06858026413690477  node count: 384  edge count: 390\n",
      "index_count: 60\n",
      "thr: 1.2967542780290904\n",
      "graph_4_12/2018-04-12 15:26:31.776115575~2018-04-12 15:41:41.913691938.txt    2.1580111972823492  count: 4540  percentage: 0.08061079545454546  node count: 426  edge count: 453\n",
      "index_count: 61\n",
      "thr: 1.2602558602860383\n",
      "graph_4_12/2018-04-12 15:41:41.913691938~2018-04-12 15:56:42.439764347.txt    1.9638939286414852  count: 3608  percentage: 0.0880859375  node count: 284  edge count: 287\n",
      "index_count: 62\n",
      "thr: 2.2789781670807576\n",
      "graph_4_12/2018-04-12 15:56:42.439764347~2018-04-12 16:11:42.990798525.txt    3.383902529330201  count: 6377  percentage: 0.10044417842741936  node count: 265  edge count: 384\n",
      "index_count: 63\n",
      "thr: 1.8002478821912966\n",
      "graph_4_12/2018-04-12 16:11:42.990798525~2018-04-12 16:26:43.075609426.txt    3.087813093078135  count: 18649  percentage: 0.06898452296401515  node count: 283  edge count: 356\n",
      "index_count: 64\n",
      "thr: 1.9207005542818716\n",
      "graph_4_12/2018-04-12 16:26:43.075609426~2018-04-12 16:42:02.002013322.txt    3.263052908041048  count: 11837  percentage: 0.07270170007861636  node count: 170  edge count: 182\n",
      "index_count: 65\n",
      "thr: 1.511287006153822\n",
      "graph_4_12/2018-04-12 16:42:02.002013322~2018-04-12 16:58:02.604248444.txt    2.762925257557619  count: 6971  percentage: 0.0667413449754902  node count: 394  edge count: 444\n",
      "index_count: 66\n",
      "thr: 1.3049456604280354\n",
      "graph_4_12/2018-04-12 16:58:02.604248444~2018-04-12 17:13:29.514953544.txt    2.21318677208511  count: 5378  percentage: 0.07097233952702703  node count: 333  edge count: 361\n",
      "index_count: 67\n",
      "thr: 1.4198877078761958\n",
      "graph_4_12/2018-04-12 17:13:29.514953544~2018-04-12 17:28:37.568314356.txt    2.7509627509230303  count: 5896  percentage: 0.05536358173076923  node count: 361  edge count: 391\n",
      "index_count: 68\n",
      "thr: 1.3222207153930905\n",
      "graph_4_12/2018-04-12 17:28:37.568314356~2018-04-12 17:44:07.048741132.txt    2.2956537732132114  count: 5606  percentage: 0.07203433388157894  node count: 314  edge count: 329\n",
      "index_count: 69\n",
      "thr: 1.6602745608818523\n",
      "graph_4_12/2018-04-12 17:44:07.048741132~2018-04-12 17:59:37.072167889.txt    3.1294660018040568  count: 15587  percentage: 0.053409402412280704  node count: 468  edge count: 523\n",
      "index_count: 70\n",
      "thr: 1.7551894589935804\n",
      "graph_4_12/2018-04-12 17:59:37.072167889~2018-04-12 18:14:42.441209885.txt    3.2749076905521415  count: 11741  percentage: 0.062314240828804345  node count: 285  edge count: 326\n",
      "index_count: 71\n",
      "thr: 1.6911351336492668\n",
      "graph_4_12/2018-04-12 18:14:42.441209885~2018-04-12 18:29:50.813709836.txt    3.1094368837311874  count: 14388  percentage: 0.05575706845238095  node count: 453  edge count: 498\n",
      "index_count: 72\n",
      "thr: 1.225616407942623\n",
      "graph_4_12/2018-04-12 18:29:50.813709836~2018-04-12 18:44:51.996770103.txt    1.955703985794724  count: 2875  percentage: 0.08257697610294118  node count: 248  edge count: 252\n",
      "index_count: 73\n",
      "thr: 1.8230495731582184\n",
      "graph_4_12/2018-04-12 18:44:51.996770103~2018-04-12 18:59:54.090374716.txt    3.399760291235997  count: 3089  percentage: 0.07734875801282051  node count: 130  edge count: 133\n",
      "index_count: 74\n",
      "thr: 1.0122298270534356\n",
      "graph_4_12/2018-04-12 18:59:54.090374716~2018-04-12 19:14:54.959663182.txt    1.8222476454993932  count: 5056  percentage: 0.0771484375  node count: 339  edge count: 367\n",
      "index_count: 75\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "thr: 1.4066184233378456\n",
      "graph_4_12/2018-04-12 19:14:54.959663182~2018-04-12 19:29:55.160238546.txt    2.3279422934842473  count: 2126  percentage: 0.07689525462962964  node count: 247  edge count: 356\n",
      "index_count: 76\n",
      "thr: 1.2311017874391914\n",
      "graph_4_12/2018-04-12 19:29:55.160238546~2018-04-12 19:44:55.907534312.txt    1.964225110697873  count: 2478  percentage: 0.08642578125  node count: 202  edge count: 207\n",
      "index_count: 77\n",
      "thr: 1.2035737058241407\n",
      "graph_4_12/2018-04-12 19:44:55.907534312~2018-04-12 19:59:56.089291407.txt    1.9047125332618886  count: 2561  percentage: 0.08624057112068965  node count: 217  edge count: 223\n",
      "index_count: 78\n",
      "thr: 1.2616397011908371\n",
      "graph_4_12/2018-04-12 19:59:56.089291407~2018-04-12 20:14:56.869858929.txt    2.1716820732249285  count: 4012  percentage: 0.07534555288461539  node count: 212  edge count: 222\n",
      "index_count: 79\n",
      "thr: 1.2931135906795248\n",
      "graph_4_12/2018-04-12 20:14:56.869858929~2018-04-12 20:30:33.002217462.txt    2.08192771617581  count: 3965  percentage: 0.08800159801136363  node count: 340  edge count: 351\n",
      "index_count: 80\n",
      "thr: 1.143760825923286\n",
      "graph_4_12/2018-04-12 20:30:33.002217462~2018-04-12 20:45:41.109871080.txt    1.7389973912408518  count: 1997  percentage: 0.09286644345238096  node count: 207  edge count: 211\n",
      "index_count: 81\n",
      "thr: 2.0905051625304365\n",
      "graph_4_12/2018-04-12 20:45:41.109871080~2018-04-12 21:01:57.550779236.txt    3.044276128776285  count: 4917  percentage: 0.11711604420731707  node count: 159  edge count: 167\n",
      "index_count: 82\n",
      "thr: 13.720920247469335\n",
      "graph_4_12/2018-04-12 21:01:57.550779236~2018-04-12 21:17:05.931449051.txt    14.238165695735221  count: 7436  percentage: 0.016065749446902654  node count: 3756  edge count: 3755\n",
      "index_count: 83\n",
      "thr: 7.454567328266351\n",
      "graph_4_12/2018-04-12 21:17:05.931449051~2018-04-12 21:32:59.560160630.txt    8.591965642700414  count: 28169  percentage: 0.10380675117924529  node count: 11640  edge count: 11660\n",
      "index_count: 84\n",
      "thr: 1.2796049624044468\n",
      "graph_4_12/2018-04-12 21:32:59.560160630~2018-04-12 21:48:00.113751799.txt    1.9664080412142328  count: 2613  percentage: 0.0945095486111111  node count: 129  edge count: 133\n",
      "index_count: 85\n",
      "thr: 1.371551546443631\n",
      "graph_4_12/2018-04-12 21:48:00.113751799~2018-04-12 22:03:13.001509745.txt    2.364777601272219  count: 3668  percentage: 0.06758549528301887  node count: 257  edge count: 268\n",
      "index_count: 86\n",
      "thr: 1.2559713215427455\n",
      "graph_4_12/2018-04-12 22:03:13.001509745~2018-04-12 22:19:00.880054693.txt    1.9029623521294043  count: 1931  percentage: 0.09924958881578948  node count: 101  edge count: 103\n",
      "index_count: 87\n",
      "thr: 1.196177894839325\n",
      "graph_4_12/2018-04-12 22:19:00.880054693~2018-04-12 22:34:01.001791765.txt    1.9733442687617027  count: 2184  percentage: 0.07899305555555555  node count: 126  edge count: 130\n",
      "index_count: 88\n",
      "thr: 1.1858118405586595\n",
      "graph_4_12/2018-04-12 22:34:01.001791765~2018-04-12 22:49:06.001717261.txt    1.9843711701306352  count: 2585  percentage: 0.07424747242647059  node count: 176  edge count: 187\n",
      "index_count: 89\n",
      "thr: 1.1756705704444212\n",
      "graph_4_12/2018-04-12 22:49:06.001717261~2018-04-12 23:04:45.002044287.txt    1.9588296440328814  count: 3500  percentage: 0.07430366847826086  node count: 267  edge count: 273\n",
      "index_count: 90\n",
      "thr: 1.296360839069314\n",
      "graph_4_12/2018-04-12 23:04:45.002044287~2018-04-12 23:19:53.001473944.txt    1.9390724520214109  count: 1220  percentage: 0.09928385416666667  node count: 81  edge count: 81\n",
      "index_count: 91\n",
      "thr: 1.0343532890364453\n",
      "graph_4_12/2018-04-12 23:19:53.001473944~2018-04-12 23:34:55.001471636.txt    1.7105704077421204  count: 921  percentage: 0.08176491477272728  node count: 84  edge count: 83\n",
      "index_count: 92\n",
      "thr: 1.1503913438820965\n",
      "graph_4_12/2018-04-12 23:34:55.001471636~2018-04-12 23:51:06.307836193.txt    1.7478956386691091  count: 1220  percentage: 0.09928385416666667  node count: 88  edge count: 90\n"
     ]
    }
   ],
   "source": [
    "node_IDF=torch.load(\"node_IDF_4_12\") \n",
    "node_IDF_3=torch.load(\"node_IDF_4_3-5\")\n",
    "# node_IDF=torch.load(\"node_IDF_4_3\")\n",
    "file_list=[]\n",
    "\n",
    "file_path=\"graph_4_12/\"\n",
    "file_l=os.listdir(\"graph_4_12/\")\n",
    "for i in file_l:\n",
    "    file_list.append(file_path+i)\n",
    "    \n",
    "# the variable names doesn't change the results.   \n",
    "y_data_4_10=[]\n",
    "df_list_4_10=[]\n",
    "history_list=[]\n",
    "tw_que=[]\n",
    "his_tw={}\n",
    "current_tw={}\n",
    "\n",
    "\n",
    "\n",
    "file_path_list=[]\n",
    "\n",
    "\n",
    "file_path=\"graph_4_12/\"\n",
    "file_l=os.listdir(\"graph_4_12/\")\n",
    "for i in file_l:\n",
    "    file_path_list.append(file_path+i)\n",
    "\n",
    "\n",
    "index_count=0\n",
    "for f_path in sorted(file_path_list):\n",
    "    f=open(f_path)\n",
    "    edge_loss_list=[]\n",
    "    edge_list=[]\n",
    "    print('index_count:',index_count)\n",
    "    \n",
    "    for line in f:\n",
    "        l=line.strip()\n",
    "        jdata=eval(l)\n",
    "        edge_loss_list.append(jdata['loss'])\n",
    "        edge_list.append([str(jdata['srcmsg']),str(jdata['dstmsg'])])\n",
    "    df_list_4_10.append(pd.DataFrame(edge_loss_list))\n",
    "    count,loss_avg,node_set,edge_set=cal_anomaly_loss(edge_loss_list,edge_list,\"graph_4_12_without_neg_edge/\")\n",
    "    current_tw={}\n",
    "    current_tw['name']=f_path\n",
    "    current_tw['loss']=loss_avg\n",
    "    current_tw['index']=index_count\n",
    "    current_tw['nodeset']=node_set\n",
    "\n",
    "    added_que_flag=False\n",
    "    for hq in history_list:\n",
    "        for his_tw in hq:\n",
    "            cal_re = cal_set_rel(current_tw['nodeset'],his_tw['nodeset'],file_list, file_list_3_5)\n",
    "            if cal_re != 0 and current_tw['name']!=his_tw['name']:\n",
    "#             if cal_set_rel_bak(current_tw['nodeset'],his_tw['nodeset'],file_l)!=0 and current_tw['name']!=his_tw['name']:\n",
    "                hq.append(copy.deepcopy(current_tw))\n",
    "                added_que_flag=True\n",
    "                break\n",
    "        if added_que_flag:\n",
    "            break\n",
    "    if added_que_flag is False:\n",
    "        temp_hq=[copy.deepcopy(current_tw)]\n",
    "        history_list.append(temp_hq)\n",
    "    index_count+=1\n",
    "    print( f_path,\"  \",loss_avg,\" count:\",count,\" percentage:\",count/len(edge_list),\" node count:\",len(node_set),\" edge count:\",len(edge_set))\n",
    "    \n",
    "    \n",
    "    \n",
    "#     for i in history_list:\n",
    "#         print(len(i))\n",
    "#         if len(i) >= 2:\n",
    "#             for tw in i:\n",
    "#                 print(tw['name'])\n",
    "#     input()\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['graph_4_12/2018-04-12 09:19:03.668714727~2018-04-12 09:34:16.330657912.txt', 'graph_4_12/2018-04-12 12:39:06.592684498~2018-04-12 12:54:44.001888457.txt', 'graph_4_12/2018-04-12 12:54:44.001888457~2018-04-12 13:09:55.026832462.txt', 'graph_4_12/2018-04-12 13:09:55.026832462~2018-04-12 13:25:06.588370709.txt', 'graph_4_12/2018-04-12 13:25:06.588370709~2018-04-12 13:40:07.178206094.txt']\n",
      "1512.6992059385764\n"
     ]
    }
   ],
   "source": [
    "for hl in history_list:\n",
    "    loss_count=0\n",
    "    for hq in hl:\n",
    "        if loss_count==0:\n",
    "            loss_count=(loss_count+1)*(hq['loss']+1)\n",
    "        else:\n",
    "            loss_count=(loss_count)*(hq['loss']+1)\n",
    "    name_list=[]\n",
    "    if loss_count>20:\n",
    "#     if loss_count>50:\n",
    "        name_list=[]\n",
    "        for i in hl:\n",
    "            name_list.append(i['name'])\n",
    "        print(name_list)\n",
    "        for i in name_list:\n",
    "            pred_label[i]=1\n",
    "        print(loss_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import average_precision_score, roc_auc_score\n",
    "\n",
    "from sklearn.metrics import roc_auc_score\n",
    "import torch\n",
    "from sklearn import preprocessing\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.metrics import confusion_matrix\n",
    "\n",
    "def plot_thr():\n",
    "    np.seterr(invalid='ignore')\n",
    "    step=0.01\n",
    "    thr_list=torch.arange(-5,5,step)\n",
    "    \n",
    "    \n",
    "\n",
    "    precision_list=[]\n",
    "    recall_list=[]\n",
    "    fscore_list=[]\n",
    "    accuracy_list=[]\n",
    "    auc_val_list=[]\n",
    "    for thr in thr_list:\n",
    "        threshold=thr\n",
    "        y_prediction=[]\n",
    "        for i in y_test_scores:\n",
    "            if i >threshold:\n",
    "                y_prediction.append(1)\n",
    "            else:\n",
    "                y_prediction.append(0)\n",
    "        precision,recall,fscore,accuracy,auc_val=classifier_evaluation(y_test, y_prediction)   \n",
    "        precision_list.append(float(precision))\n",
    "        recall_list.append(float(recall))\n",
    "        fscore_list.append(float(fscore))\n",
    "        accuracy_list.append(float(accuracy))\n",
    "        auc_val_list.append(float(auc_val))\n",
    "\n",
    "    max_fscore=max(fscore_list)\n",
    "    max_fscore_index=fscore_list.index(max_fscore)\n",
    "    print(max_fscore_index)\n",
    "    print(\"max threshold:\",thr_list[max_fscore_index])\n",
    "    print('precision:',precision_list[max_fscore_index])\n",
    "    print('recall:',recall_list[max_fscore_index])\n",
    "    print('fscore:',fscore_list[max_fscore_index])\n",
    "    print('accuracy:',accuracy_list[max_fscore_index])    \n",
    "    print('auc:',auc_val_list[max_fscore_index])\n",
    "    \n",
    "         \n",
    "#     precision_list=torch.tensor(precision_list)   \n",
    "#     recall_list=torch.tensor(recall_list)   \n",
    "#     fscore_list=torch.tensor(fscore_list)   \n",
    "#     accuracy_list=torch.tensor(accuracy_list)   \n",
    "#     auc_val_list=torch.tensor(auc_val_list)   \n",
    "\n",
    "    \n",
    "\n",
    "\n",
    "    \n",
    "    # plt.scatter(attack_x, attack_y, s=20, c='r', label='Attack graph',marker='*')\n",
    "    # plt.scatter(bengin_x, bengin_y, s=20, c='g', label='Bengin graph',marker='1')\n",
    "    # plt.scatter(bengin_x, bengin_y, s=20, c='g', label='Bengin graph',marker='1')\n",
    "\n",
    "    plt.plot(thr_list,precision_list,color='red',label='precision',linewidth=2.0,linestyle='-')\n",
    "    plt.plot(thr_list,recall_list,color='orange',label='recall',linewidth=2.0,linestyle='solid')\n",
    "    plt.plot(thr_list,fscore_list,color='y',label='F-score',linewidth=2.0,linestyle='dashed')\n",
    "    plt.plot(thr_list,accuracy_list,color='g',label='accuracy',linewidth=2.0,linestyle='dashdot')\n",
    "    plt.plot(thr_list,auc_val_list,color='b',label='auc_val',linewidth=2.0,linestyle='dotted')\n",
    "    # '-', '--', '-.', ':', 'None', ' ', '', 'solid', 'dashed', 'dashdot', 'dotted'\n",
    "\n",
    "\n",
    "    # plt.scatter(turnovers, graph_loss, c=color)\n",
    "    plt.xlabel(\"Threshold\", fontdict={'size': 16})\n",
    "    plt.ylabel(\"Rate\", fontdict={'size': 16})\n",
    "    plt.title(\"Different evaluation Indicators by varying threshold value\", fontdict={'size': 12})\n",
    "    plt.legend(loc='best', fontsize=12, markerscale=0.5)\n",
    "    plt.show()\n",
    "\n",
    "def classifier_evaluation(y_test, y_test_pred):\n",
    "    # groundtruth, pred_value\n",
    "    tn, fp, fn, tp =confusion_matrix(y_test, y_test_pred).ravel()\n",
    "#     tn+=100\n",
    "#     print(clf_name,\" : \")\n",
    "    print('tn:',tn)\n",
    "    print('fp:',fp)\n",
    "    print('fn:',fn)\n",
    "    print('tp:',tp)\n",
    "    precision=tp/(tp+fp)\n",
    "    recall=tp/(tp+fn)\n",
    "    accuracy=(tp+tn)/(tp+tn+fp+fn)\n",
    "    fscore=2*(precision*recall)/(precision+recall)    \n",
    "    auc_val=roc_auc_score(y_test, y_test_pred)\n",
    "    print(\"precision:\",precision)\n",
    "    print(\"recall:\",recall)\n",
    "    print(\"fscore:\",fscore)\n",
    "    print(\"accuracy:\",accuracy)\n",
    "    print(\"auc_val:\",auc_val)\n",
    "    return precision,recall,fscore,accuracy,auc_val\n",
    "\n",
    "def minmax(data):\n",
    "    min_val=min(data)\n",
    "    max_val=max(data)\n",
    "    ans=[]\n",
    "    for i in data:\n",
    "        ans.append((i-min_val)/(max_val-min_val))\n",
    "    return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "y=[]\n",
    "y_pred=[]\n",
    "for i in labels:\n",
    "    y.append(labels[i])\n",
    "    y_pred.append(pred_label[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tn: 216\n",
      "fp: 2\n",
      "fn: 0\n",
      "tp: 9\n",
      "precision: 0.8181818181818182\n",
      "recall: 1.0\n",
      "fscore: 0.9\n",
      "accuracy: 0.9911894273127754\n",
      "auc_val: 0.9954128440366973\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(0.8181818181818182, 1.0, 0.9, 0.9911894273127754, 0.9954128440366973)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classifier_evaluation(y,y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Count the attack edges numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def keyword_hit(line):\n",
    "    attack_nodes=[\n",
    "            '/home/admin/clean',\n",
    "            '/dev/glx_alsa_675',\n",
    "            '/home/admin/profile',\n",
    "#             '/var/log/mail',  \n",
    "            '/tmp/memtrace.so',\n",
    "            '/var/log/xdev',\n",
    "             '/var/log/wdev',\n",
    "            'gtcache',\n",
    "#             'firefox',\n",
    "        '161.116.88.72',\n",
    "        '146.153.68.151',\n",
    "        '145.199.103.57',\n",
    "        '61.130.69.232',\n",
    "        '5.214.163.155',\n",
    "        '104.228.117.212',\n",
    "        '141.43.176.203',\n",
    "        '7.149.198.40',\n",
    "        '5.214.163.155',\n",
    "        '149.52.198.23',\n",
    "        ]\n",
    "    flag=False\n",
    "    for i in attack_nodes:\n",
    "        if i in line:\n",
    "            flag=True\n",
    "            break\n",
    "    return flag\n",
    "\n",
    "\n",
    "\n",
    "files=[]\n",
    "temp_file=[\n",
    "        '2018-04-10 13:31:14.548738409~2018-04-10 13:46:36.161065223.txt',\n",
    "        '2018-04-10 14:02:17.001271389~2018-04-10 14:17:34.001373488.txt',\n",
    "        '2018-04-10 14:17:34.001373488~2018-04-10 14:33:18.350772859.txt',\n",
    "        '2018-04-10 14:33:18.350772859~2018-04-10 14:48:47.320442910.txt',\n",
    "        '2018-04-10 14:48:47.320442910~2018-04-10 15:03:54.307022037.txt',\n",
    "]\n",
    "for f in temp_file:\n",
    "    files.append(\"./graph_4_10/\"+f)\n",
    "    \n",
    "    \n",
    "temp_file=[\n",
    "         '2018-04-12 12:39:06.592684498~2018-04-12 12:54:44.001888457.txt',\n",
    "        '2018-04-12 12:54:44.001888457~2018-04-12 13:09:55.026832462.txt',\n",
    "        '2018-04-12 13:09:55.026832462~2018-04-12 13:25:06.588370709.txt',\n",
    "        '2018-04-12 13:25:06.588370709~2018-04-12 13:40:07.178206094.txt',\n",
    "]    \n",
    "for f in temp_file:\n",
    "    files.append(\"./graph_4_12/\"+f)\n",
    "    \n",
    "attack_edge_count=0\n",
    "for fpath in tqdm(files):\n",
    "    f=open(fpath)\n",
    "    for line in f:\n",
    "        if keyword_hit(line):\n",
    "            attack_edge_count+=1\n",
    "print(attack_edge_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  0%|                                                                                                    | 0/9 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.37880797462106824\n",
      "0.7720015244646495\n",
      "thr: 1.5368102613180425\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 22%|████████████████████▍                                                                       | 2/9 [00:02<00:08,  1.18s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.3190400899997106\n",
      "0.7338338201816546\n",
      "thr: 1.4197908202721923\n",
      "0.11138730359408652\n",
      "0.26162931634641656\n",
      "thr: 0.5038312781137114\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 44%|████████████████████████████████████████▉                                                   | 4/9 [00:28<00:38,  7.69s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.3557301810408585\n",
      "0.8170120593502175\n",
      "thr: 1.581248270066185\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 56%|███████████████████████████████████████████████████                                         | 5/9 [00:30<00:21,  5.40s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.3347016566220287\n",
      "0.7452701189601297\n",
      "thr: 1.4526068350622232\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 67%|█████████████████████████████████████████████████████████████▎                              | 6/9 [00:30<00:11,  3.79s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.31986105850509944\n",
      "0.8227656165567785\n",
      "thr: 1.5540094833402671\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 78%|███████████████████████████████████████████████████████████████████████▌                    | 7/9 [00:31<00:05,  2.84s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2738504958599673\n",
      "0.7165869564867917\n",
      "thr: 1.348730930590155\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 89%|█████████████████████████████████████████████████████████████████████████████████▊          | 8/9 [00:32<00:02,  2.22s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5851781187296052\n",
      "1.9001599040214228\n",
      "thr: 5.435417974761739\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████| 9/9 [00:33<00:00,  3.76s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.43680985010152273\n",
      "0.8076026072484177\n",
      "thr: 1.6482137609741492\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "from graphviz import Digraph\n",
    "import networkx as nx\n",
    "import datetime\n",
    "import community.community_louvain as community_louvain\n",
    "from tqdm import tqdm\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# Some common path abstraction for visualization\n",
    "replace_dic = {\n",
    "        '/run/shm/':'/run/shm/*',\n",
    "        #     '/home/admin/.cache/mozilla/firefox/pe11scpa.default/cache2/entries/':'/home/admin/.cache/mozilla/firefox/pe11scpa.default/cache2/entries/*',\n",
    "        '/home/admin/.cache/mozilla/firefox/':'/home/admin/.cache/mozilla/firefox/*',\n",
    "        '/home/admin/.mozilla/firefox':'/home/admin/.mozilla/firefox*',\n",
    "        '/data/replay_logdb/':'/data/replay_logdb/*',\n",
    "        '/home/admin/.local/share/applications/':'/home/admin/.local/share/applications/*',\n",
    "        '/usr/share/applications/':'/usr/share/applications/*',\n",
    "        '/lib/x86_64-linux-gnu/':'/lib/x86_64-linux-gnu/*',\n",
    "        '/proc/':'/proc/*',\n",
    "        '/stat':'*/stat',\n",
    "        '/etc/bash_completion.d/':'/etc/bash_completion.d/*',\n",
    "        '/usr/bin/python2.7':'/usr/bin/python2.7/*',\n",
    "        '/usr/lib/python2.7':'/usr/lib/python2.7/*',\n",
    "}\n",
    "\n",
    "\n",
    "def replace_path_name(path_name):\n",
    "    for i in replace_dic:\n",
    "        if i in path_name:\n",
    "            return replace_dic[i]\n",
    "    return path_name\n",
    "\n",
    "\n",
    "# Users should manually put the detected anomalous time windows here\n",
    "attack_list = [\n",
    "        'graph_4_10/2018-04-10 13:31:14.548738409~2018-04-10 13:46:36.161065223.txt',\n",
    "        'graph_4_10/2018-04-10 14:02:17.001271389~2018-04-10 14:17:34.001373488.txt',\n",
    "        'graph_4_10/2018-04-10 14:17:34.001373488~2018-04-10 14:33:18.350772859.txt',\n",
    "        'graph_4_10/2018-04-10 14:33:18.350772859~2018-04-10 14:48:47.320442910.txt',\n",
    "        'graph_4_10/2018-04-10 14:48:47.320442910~2018-04-10 15:03:54.307022037.txt',\n",
    "    \n",
    "        'graph_4_12/2018-04-12 12:39:06.592684498~2018-04-12 12:54:44.001888457.txt', \n",
    "        'graph_4_12/2018-04-12 12:54:44.001888457~2018-04-12 13:09:55.026832462.txt', \n",
    "        'graph_4_12/2018-04-12 13:09:55.026832462~2018-04-12 13:25:06.588370709.txt', \n",
    "        'graph_4_12/2018-04-12 13:25:06.588370709~2018-04-12 13:40:07.178206094.txt'\n",
    "]\n",
    "\n",
    "original_edges_count = 0\n",
    "graphs = []\n",
    "gg = nx.DiGraph()\n",
    "count = 0\n",
    "for path in tqdm(attack_list):\n",
    "    if \".txt\" in path:\n",
    "        line_count = 0\n",
    "        node_set = set()\n",
    "        tempg = nx.DiGraph()\n",
    "        f = open(path, \"r\")\n",
    "        edge_list = []\n",
    "        for line in f:\n",
    "            count += 1\n",
    "            l = line.strip()\n",
    "            jdata = eval(l)\n",
    "            edge_list.append(jdata)\n",
    "\n",
    "        edge_list = sorted(edge_list, key=lambda x: x['loss'], reverse=True)\n",
    "        original_edges_count += len(edge_list)\n",
    "\n",
    "        loss_list = []\n",
    "        for i in edge_list:\n",
    "            loss_list.append(i['loss'])\n",
    "        loss_mean = mean(loss_list)\n",
    "        loss_std = std(loss_list)\n",
    "        print(loss_mean)\n",
    "        print(loss_std)\n",
    "        thr = loss_mean + 1.5 * loss_std\n",
    "        print(\"thr:\", thr)\n",
    "        for e in edge_list:\n",
    "            if e['loss'] > thr:\n",
    "                tempg.add_edge(str(hashgen(replace_path_name(e['srcmsg']))),\n",
    "                               str(hashgen(replace_path_name(e['dstmsg']))))\n",
    "                gg.add_edge(str(hashgen(replace_path_name(e['srcmsg']))), str(hashgen(replace_path_name(e['dstmsg']))),\n",
    "                            loss=e['loss'], srcmsg=e['srcmsg'], dstmsg=e['dstmsg'], edge_type=e['edge_type'],\n",
    "                            time=e['time'])\n",
    "\n",
    "\n",
    "partition = community_louvain.best_partition(gg.to_undirected())\n",
    "\n",
    "# Generate the candidate subgraphs based on community discovery results\n",
    "communities = {}\n",
    "max_partition = 0\n",
    "for i in partition:\n",
    "    if partition[i] > max_partition:\n",
    "        max_partition = partition[i]\n",
    "for i in range(max_partition + 1):\n",
    "    communities[i] = nx.DiGraph()\n",
    "for e in gg.edges:\n",
    "    communities[partition[e[0]]].add_edge(e[0], e[1])\n",
    "    communities[partition[e[1]]].add_edge(e[0], e[1])\n",
    "\n",
    "\n",
    "# Define the attack nodes. They are **only be used to plot the colors of attack nodes and edges**.\n",
    "# They won't change the detection results.\n",
    "def attack_edge_flag(msg):\n",
    "    attack_nodes = [\n",
    "        '/home/admin/clean',\n",
    "        '/dev/glx_alsa_675',\n",
    "        '/home/admin/profile',\n",
    "        '/var/log/xdev',\n",
    "        '/etc/passwd',\n",
    "        '161.116.88.72',\n",
    "        '146.153.68.151',\n",
    "        '/var/log/mail',\n",
    "        '/tmp/memtrace.so',\n",
    "        #         '/tmp',\n",
    "        '/var/log/xdev',\n",
    "        '/var/log/wdev',\n",
    "        'gtcache',\n",
    "        'firefox',\n",
    "        #         '/var/log',\n",
    "    ]\n",
    "    flag = False\n",
    "    for i in attack_nodes:\n",
    "        if i in str(msg):\n",
    "            flag = True\n",
    "    return flag\n",
    "\n",
    "\n",
    "# Plot and render candidate subgraph\n",
    "os.system(f\"mkdir -p ./graph_visual/\")\n",
    "graph_index = 0\n",
    "for c in communities:\n",
    "    dot = Digraph(name=\"MyPicture\", comment=\"the test\", format=\"pdf\")\n",
    "    dot.graph_attr['rankdir'] = 'LR'\n",
    "\n",
    "    for e in communities[c].edges:\n",
    "        try:\n",
    "            temp_edge = gg.edges[e]\n",
    "            srcnode = e['srcnode']\n",
    "            dstnode = e['dstnode']\n",
    "        except:\n",
    "            pass\n",
    "\n",
    "        if True:\n",
    "            # source node\n",
    "            if \"'subject': '\" in temp_edge['srcmsg']:\n",
    "                src_shape = 'box'\n",
    "            elif \"'file': '\" in temp_edge['srcmsg']:\n",
    "                src_shape = 'oval'\n",
    "            elif \"'netflow': '\" in temp_edge['srcmsg']:\n",
    "                src_shape = 'diamond'\n",
    "            if attack_edge_flag(temp_edge['srcmsg']):\n",
    "                src_node_color = 'red'\n",
    "            else:\n",
    "                src_node_color = 'blue'\n",
    "            dot.node(name=str(hashgen(replace_path_name(temp_edge['srcmsg']))), label=str(\n",
    "                replace_path_name(temp_edge['srcmsg']) + str(\n",
    "                    partition[str(hashgen(replace_path_name(temp_edge['srcmsg'])))])), color=src_node_color,\n",
    "                     shape=src_shape)\n",
    "\n",
    "            # destination node\n",
    "            if \"'subject': '\" in temp_edge['dstmsg']:\n",
    "                dst_shape = 'box'\n",
    "            elif \"'file': '\" in temp_edge['dstmsg']:\n",
    "                dst_shape = 'oval'\n",
    "            elif \"'netflow': '\" in temp_edge['dstmsg']:\n",
    "                dst_shape = 'diamond'\n",
    "            if attack_edge_flag(temp_edge['dstmsg']):\n",
    "                dst_node_color = 'red'\n",
    "            else:\n",
    "                dst_node_color = 'blue'\n",
    "            dot.node(name=str(hashgen(replace_path_name(temp_edge['dstmsg']))), label=str(\n",
    "                replace_path_name(temp_edge['dstmsg']) + str(\n",
    "                    partition[str(hashgen(replace_path_name(temp_edge['dstmsg'])))])), color=dst_node_color,\n",
    "                     shape=dst_shape)\n",
    "\n",
    "            if attack_edge_flag(temp_edge['srcmsg']) and attack_edge_flag(temp_edge['dstmsg']):\n",
    "                edge_color = 'red'\n",
    "            else:\n",
    "                edge_color = 'blue'\n",
    "            dot.edge(str(hashgen(replace_path_name(temp_edge['srcmsg']))),\n",
    "                     str(hashgen(replace_path_name(temp_edge['dstmsg']))), label=temp_edge['edge_type'],\n",
    "                     color=edge_color)\n",
    "\n",
    "    dot.render(f'./graph_visual/subgraph_' + str(graph_index), view=False)\n",
    "    graph_index += 1\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "225.797px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
